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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+
  • Graph API for Entra ID (Azure AD) Object Management
  • A Comprehensive Guide to IAM in Object Storage

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Ethical AI in Agile
  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
28.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

  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+
  • Graph API for Entra ID (Azure AD) Object Management
  • A Comprehensive Guide to IAM in Object Storage

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!