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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 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
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server

Trending

  • A Spring Boot App With Half the Startup Time
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot
  • Encryption Won't Survive Quantum Computing: What to Do?
  • AI Assessments Are Everywhere
  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.9K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 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
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server

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