DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > DataWeave Interview Question: Reverse an Array After Combining Its Elements

DataWeave Interview Question: Reverse an Array After Combining Its Elements

This article will help you practice your DataWeave skills in MuleSoft. Use the article as a tutorial for learning more about DataWeave.

Gaurav Dubey user avatar by
Gaurav Dubey
CORE ·
Jan. 20, 22 · Integration Zone · Tutorial
Like (3)
Save
Tweet
4.89K Views

Join the DZone community and get the full member experience.

Join For Free

This article will help you practice your DataWeave skills in MuleSoft. Use the article as a tutorial for learning more about DataWeave.

Let's get started. 

Input:

JSON
 
[

"uat", ["sit", "qa", "prod"]

]

Output:

[

  "prod",

  "qa",

  "sit",

  "uat"

]


Let's talk about the solution now.

In the first step, we will combine all the elements of an array into a single array using flatten function.

Step 1 Code:

DataWeave Script

 
%dw 2.0

output application/json

var combinedPayload = (flatten(payload))

---

combinedPayload


Step 1 Output:

JSON
 

[

  "uat",

  "sit",

  "qa",

  "prod"

]


In the second step, we will reverse the contents of an array using the range selector.

Step 2 Code:

DataWeave Script

 

%dw 2.0

output application/json

var combinedPayload = (flatten(payload))

---

combinedPayload[(sizeOf(combinedPayload)) -1 to 0]


Step 2 Output:

JSON
 
[

  "prod",

  "qa",

  "sit",

  "uat"

]


We have used sizeOf function to get the size of our combinedPayload along with the range selector to get the array in reverse format.

Happy Learning!

Data structure Element Interview (journalism)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Portfolio Architecture Examples: Retail Collection
  • 10 Steps to Become an Outstanding Java Developer

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo