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

  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption
  • The SDLC After AI
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems

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: 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
·
Mar. 06, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.0K 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

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

  • 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