The MapObject function in Mulesfot is one of the most commonly used functions that allows you to transform data from one format to another within a Mule application. It can be used to convert data between JSON, XML, and other formats, as well as to map data between different structures or schemas.
The MapObject is used to iterate over each key-value pair in a JSON object. To use the MapObject function, you will need to create a mapping configuration file that defines the source and target data structures, as well as the rules for transforming the data from one format to the other. You can then use the MapObject function to apply this mapping to your data, transforming it from the source format to the target format.
The syntax of the mapObject function is
payload mapObject (value,key,index) -> {}
Here key is the JSON key in the object, the value is the JSON value in the object and the index is the position of the key-value pair in the object which starts from 0.
We will see the usage of mapObject with examples. Let us consider the following JSON input data.
{ "name" : "bigDataers", "country" : "UnitedStates" }
The basic thing that we can do with mapObject is iterate over each key-value pair of a JSON object.
%dw 2.0 output application/json --- payload mapObject (value, key, index) -> { (key): value }
The execution of this DW code will produce the result same as the input. Note that the key should be wrapped around braces to get the actual key. If you do not wrap it around the braces, then it will result in the literal key string.
The $ symbol refers to the values, $$ symbol refers to the keys, and $$$ symbol points to the position in the JSON object. The following example shows the usage of these symbols.
%dw 2.0 output application/json --- payload mapObject () -> { ($$): $ }
Upon executing this dataweave code will generate the output the same as the input. Now we will see the usage of $$$ with another example.
%dw 2.0 output application/json --- payload mapObject () -> { ($$): $$$ }
This will print the following output.
{ "name": 0, "country": 1 }
Here 0 and 1 are the positions of the keys "name" and "country" in the input JSON object.
We can add new fields to the JSON object while iterating through it. The following MapObject code function illustrates this behavior.
%dw 2.0 output application/json --- payload mapObject (value, key) -> { (key): value, ("state" : "Washington") if (index == 0) }
The output of the above DW code execution is
{ "name": "bigDataers", "state": "Washington", "country": "UnitedStates" }
Here, we have added the new field state. Also, note that we have used the if condition to add the new field only when the index is zero. If we have not used this if condition, then the new field gets added multiple times.
Sometimes we only need to extract keys from the JSON, then in such cases, we can use the namesOf function. The usage of namesOf function is shown below.
%dw 2.0 output application/json --- namesOf(payload)
It returns an array of keys as shown in the following output.
[ "name", "country" ]
We can iterate over this array result using the map function that is illustrated below.
%dw 2.0 output application/json var p = { "name" : "bigDataers", "country" : "UnitedStates" } --- [ "name", "country"] map () -> { ($) : p[$] }
This DW code converts the object into an array with each key-value pair in a separate object as shown in the following result.
[ { "name": "bigDataers" }, { "country": "UnitedStates" } ]
We can write complex dataweave code by using various core functions. Use the appropriate functions depending on the business requirement. You can also checkout my other articles on dataweave transformations and Map functions to learn more about the Mulesoft core functions.
To conclude, the mule mapObject is used to iterate over an object of key-value pairs, and also we can do data transformations like adding new fields and converting from one format to another format.