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

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

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

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

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Start Coding With Google Cloud Workstations
  • Chaos Engineering for Microservices
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  1. DZone
  2. Coding
  3. Languages
  4. DataWeave: The mapObject Function

DataWeave: The mapObject Function

The mapObject DataWeave function transform data contained in an object. It iterates over each key/value pair, applies a transformation to each key, and outputs an object.

By 
Alex Theedom user avatar
Alex Theedom
·
Mar. 06, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

Main Points

  • The mapObject takes an object as an input and outputs an object
  • The mapObject transforms each key/value pair in the object

What Is the mapObject Function Used For?

The mapObject function is used to transform the data contained in an object, in the same way that the map function transforms the elements of an array. It does this by iterating over each key/value pair in the object and applying a transformation to each key and value. The result of each transformation is formed into an object.

How Is the Transformation Described?

The transformation that is applied to each key and value is described with a DataWeave script. For example, you can refer to the key, value, and index of the object to be transformed in the DataWeave script and apply a transformation such as the upper() function.

How Does the mapObject Work?

The mapObject function takes two inputs, the object to transform and the transformation script which is presented as a lambda expression. The object is always to the left of the mapObject function and the lambda expression is always on the right of the mapObject function.

Simple Example of the mapObject Function

In the figures below the mapObject function iterations over the keys and values of the input object (figure 1), and passes each value and key into the lambda expression on the right of the mapObject function (figure 2). The lambda expression extracts the key, value, and index of each key/value pair and organises them into a new object.

Figure 1: The input object payload to the mapObject function.

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "pet: "cat",
3
  "name": "Ponsonby",
4
  "likes": [ "mice", "butterflies" ]
5
}



Figure 2: The mapObject function and transformation DataWeave script.

Java
 




x


 
1
payload mapObject (value, key, index) -> element: {
2
                                            index: index,
3
                                            key: key,
4
                                            value: value
5
                                          }



The output of the mapObject mapping transformation collates the input object's key, value, and index as shown in figure 3.

Figure 3: The output from the mapObject transformation.

JSON
 




x


 
1
{
2
  "element": {
3
    "index": 0,
4
    "key": "pet",
5
    "value": "cat"
6
  },
7
  "element": {
8
    "index": 1,
9
    "key": "name",
10
    "value": "Ponsonby"
11
  },
12
  "element": {
13
    "index": 2,
14
    "key": "likes",
15
    "value": [ "mice", "butterflies" ]
16
  }
17
}



Use the Built-in Default Short Cuts

The key, value, and index can be replaced by the built-in shortcuts $$, $ and $$$ respectively. The above example can be sampled as shown in figure 4.

Figure 4: Use the built-in default to shorten the code.

Java
 




xxxxxxxxxx
1


 
1
payload mapObject element: { index: $$$, key: $$, value: $ }



DataWeave Functions

DataWeave functions provide transformation capabilities that can transform arrays and objects. For example, the map DataWeave function transforms each element in an array, it accepts an array as input and a DataWeave expression and outputs an array of transformed elements. Another example is the pluck DataWeave function, which iterates over each key/value pair of an object; retrieves key, value, and index; and outputs an array of transformed elements.

Object (computer science)

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

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!