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

  • Why Successful Tech Transformation Starts With Clarity
  • Building a Windows App for a Resilient SSH SOCKS Proxy and Selective Browser Routing
  • How to Evaluate Coding Agents With Harbor Framework, Python, Docker
  • Member Spotlight: Shai Almong

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. 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
DZone Core CORE ·
Jul. 20, 21 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
29.4K 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

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

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