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

  • Testing Business Programs Without Constructing Domain Objects
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Trending

  • Can Your Team Name the Work It Already Runs With AI?
  • How to Verify Response Data in API Testing With Playwright TypeScript
  • 6 Techniques To Reduce LLM API Costs With the Python Library
  • Prompt Caching Doesn't Save Money on Turn One
  1. DZone
  2. Coding
  3. Languages
  4. Dataweave 2: Objects and Arrays

Dataweave 2: Objects and Arrays

In this article, we describe, discuss, and walk through two different Dataweave 2 approaches (pluck and reduce) to convert between Objects and Arrays.

By 
Susmit Dey user avatar
Susmit Dey
DZone Core CORE ·
Jul. 20, 21 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
29.3K Views

Join the DZone community and get the full member experience.

Join For Free

Anybody dealing with Dataweave scripts will often come across situations where an Object needs to be converted into an Array or vice-versa. The Dataweave 2 examples below describe how to convert between Objects and Arrays.

Example 1: Converting an Object to an Array

This example uses the core Dataweave function pluck; it is useful in mapping an object into an array, pluck iterates over an object and returns an array of keys, values, or indices from the object. It is an alternative to mapObject, which is similar but returns an object, instead of an array.

Ref: https://docs.mulesoft.com/mule-runtime/4.3/dw-core-functions-pluck

Input:

JSON
 
{
  "key1" : "value1",
  "key2" : "value2",
  "key3" : "value3"
}

Dataweave:

JavaScript
 
%dw 2.0
output application/json
---
payload pluck ((value, key, index) -> 
{
    "key" : key,
    "value" : value,
    "index" : index
}
)

Output:

JSON
 
[
  {
    "key": "key1",
    "value": "value1",
    "index": 0
  },
  {
    "key": "key2",
    "value": "value2",
    "index": 1
  },
  {
    "key": "key3",
    "value": "value3",
    "index": 2
  }
]

Example 2: Converting an Array to an Object

This example uses the core Dataweave function reduce; it is useful in applying a reduction expression to the elements in an array. For each element of the input array, in order to, reduce applies the reduction lambda expression (function), then replace the accumulator with the new result. The lambda expression can use both the current input array element and the current accumulator value.

Ref: https://docs.mulesoft.com/mule-runtime/4.3/dw-core-functions-reduce

Input: 

Java
 
[
  {
    "key1": "value1"
  },
  {
    "key2": "value2"
  },
  {
    "key3": "value3"
  }
]

Dataweave:

JavaScript
 
%dw 2.0
output application/json
---
payload reduce ($ ++ $$)

Output:

JSON
 
{
  "key3": "value3",
  "key2": "value2",
  "key1": "value1"
}

Hope this helps!

Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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