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

  • Adding Two Hours in DataWeave: Mule 4
  • DataWeave Interview Question: Concatenate Elements of an Array
  • Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator
  • SmartXML: An Alternative to XPath for Complex XML Files

Trending

  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • Designing API-First EMR Architectures in .NET: Enabling Modular Growth in Compliance-Driven Systems
  • Stop Running Two Data Systems for One Agent Query
  • Key Takeaways From Integrating a RAG Application With LangSmith
  1. DZone
  2. Coding
  3. Languages
  4. DataWeave: The map Function Explained

DataWeave: The map Function Explained

The DataWeave map function is used to transform data contained in an array. It does this by iterating over the elements in the array and applying a transformation to each element.

By 
Alex Theedom user avatar
Alex Theedom
·
Jan. 11, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
23.1K Views

Join the DZone community and get the full member experience.

Join For Free

Main Points:

  • The map function transforms data
  • If iterates over the elements in an array 
  • It applies a transformation to each element
  • Applies only to array input
  • It can only output an array

What Is the map Function Used For?

The map function is used to transform the data contained in an array. It does this by iterating over the elements in the array and applying a transformation to each element. The result of the transformation is collected together and output as an array of transformed elements. If you are familiar with Java 8 functional programming approach, a comparison can be made with the the map() method of the Stream class, find out more in my article Contrast DataWeave and Java mapping operations.

How Is the Transformation Described?

The transformation that is applied to each element is described with DataWeave. For example, to change a text value in the source array use the upper() built in function.

How Does the map Function Work? 

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

Simple Example of the map Function

In figure 1 the map function iterations over the array, element by element, and passes each element into the lambda expression on the right of the map function. The lambda expression passes the element in to the upper() function which transforms the element to uppercase. 

Figure 1: The array is transformed by the lambda expression


The map function iterates over every element in the array and applies the transformation lambda to each element. The output of each transformation is collected together into an array. Figure 2 shows the output.

Figure 2: Output of the map transformation

The map Function Defaults 

The lambda function can be simplified by using the built in defaults for element and index, the single dollar $ and double $$ respectively. The map example in figure 1 would be transformed as shown in figure 3.

Figure 3: Use the $ default to represent element
 

Transform a JSON Array With the map Function

The map function can be used to transform an array of JSON objects. The transformation expression is applied to each object in the array. The dollar (element) refers to the entire object so that a field of that object can be referenced using the single selector notion. Consider the array of JSON objects in the figure 4.

JSON
 




x
10


 
1
[
2
  {
3
    "symbol": "AAPL",
4
    "price": 119.36
5
  },
6
  {
7
    "symbol": "CRM",
8
    "price": 258.58
9
  }
10
]


Figure 4: An array of JSON objects


Each object has two fields symbol and price, which have associated values. They can be referred to by using either element and index or the default $ and $$.

Java
 




xxxxxxxxxx
1


 
1
payload map (element, index) -> {
2
  'code' : element.symbol,
3
  'price' : element.price
4
}


Figure 5: Transformation of JSON array - complete form


Java
 




xxxxxxxxxx
1


 
1
payload map {
2
  'code' : $.symbol,
3
  'price' : $.price
4
}


Figure 6: Transformation of JSON array - using defaults


JSON
 




xxxxxxxxxx
1
10


 
1
[
2
  {
3
    "symbol": "AAPL",
4
    "price": 119.36
5
  },
6
  {
7
    "symbol": "CRM",
8
    "price": 258.58
9
  }
10
]


Figure 7: Output of figure 5 and 6

Advanced Example of the map Function

A more complex example would transform elements' values, such as shown in figure 8.

Java
 




x


 
1
payload map {
2
  'code' : lower($.symbol),
3
  'price' : $.price as String {format: "£#,###.00"}
4
}



Figure 8: Transform each element in the JSON array

JSON
 




xxxxxxxxxx
1
10


 
1
[
2
  {
3
    "code": "aapl",
4
    "price": "£119.36"
5
  },
6
  {
7
    "code": "crm",
8
    "price": "£258.58"
9
  }
10
]



Figure 9: The output from the map transformation in figure 8

DataWeave mapObject Function

The DataWeave mapObject function operates on an Object consisting of key and value pair just as the map function does over an array. Each key and value pair is transformed by the given transformation script and the resulting transformation is output to an object.

Data structure Element JSON 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

  • Adding Two Hours in DataWeave: Mule 4
  • DataWeave Interview Question: Concatenate Elements of an Array
  • Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator
  • SmartXML: An Alternative to XPath for Complex XML Files

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