DZone
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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • DataWeave Interview Question: Concatenate Elements of an Array
  • Reversing an Array: An Exploration of Array Manipulation
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window

Trending

  • How to Tell If Code Is AI-Generated (and What to Ask)
  • How Has AI Impacted Code Obfuscation?
  • Testing Asynchronous Systems, Part 1: Mapping the System and Classifying Its Asynchrony
  • Seven AI Adoption Failures and How to Overcome Them

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. 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.

By 
Gaurav Dubey
·
Jan. 20, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Related

  • DataWeave Interview Question: Concatenate Elements of an Array
  • Reversing an Array: An Exploration of Array Manipulation
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook