Enhance JSON Data with DataWeave's Update Operator | MuleSoft Guide

Introduction

In this post, you will learn to use DataWeave to convert a JSON input with book-related data. The conversion creates another JSON output that includes the price and author of each title. You will also learn how to use DataWeave’s update operator, variables, functions, imports, types, and namespaces.  

Prerequisites

Basic understanding of DataWeave syntax and data structures is required in order to follow along with this article. For additional information, see the DataWeave Language Introduction or the DataWeave Quick start. Additionally, you can play with DataWeave code using the DataWeave Playground or the DataWeave Interactive Learning Environment.    

Input and Output

An array of objects with the key response is the input for this transformation. It is a JSON object. Two keys are assigned to each object in the array: s and Date. An array of objects called the “s key” holds book-related data such title, author, and status. A string that indicates the date of the answer is the Date key. A sample of the input is provided here:  

INPUT

[  {    "s": [      {        "book": "s",        "title": "0.0",        "status": "SUCCESS",        "price": "1"      },      {        "book": "ss",        "title": "0.0",        "status": "SUCCESS",        "price": "11"      }    ],    "Date": "2024-01-01"  },  {    "s": [      {        "book": "s",        "title": "0.0",        "status": "SUCCESS",        "price": "1"      },      {        "book": "ss",        "title": "0.0",        "status": "SUCCESS",        "price": "11"      }    ],    "Date": "2024-01-01"  }]

After this modification, the result is a JSON object with the same structure as the input. One more key—price—is added to each book object in the s array. Another JSON object is defined as a variable in the script. It is where the value for that key comes from. This is an illustration of the output:    

OUTPUT

[  {    "s": [      {        "book": "s",        "title": "0.0",        "status": "SUCCESS",        "price": "1"      },      {        "book": "ss",        "title": "0.0",        "status": "SUCCESS",        "price": "11"      }    ],    "Date": "2024-01-01"  },  {    "s": [      {        "book": "s",        "title": "0.0",        "status": "SUCCESS",        "price": "1"      },      {        "book": "ss",        "title": "0.0",        "status": "SUCCESS",        "price": "11"      }    ],    "Date": "2024-01-01"  }]

DataWeave Script

Here is the DataWeave script that carries out this conversion:   This is the DataWeave script used to perform the conversion:  

%dw 2.0import * from dw::util::Valuesvar res2 = {  "s": [    {      "price": "1",      "author": "ss"    },    {      "price": "11",      "author": "bb"    }  ],  "Date": "2024-01-01"}output application/json  ---payload.fResponse map ((item, index) -> item update {    case .s -> $ map ($ ++ (      price: (res2.s[$$] default {}).price    ))  })

Let’s break down the script into different parts and explain what each part does.

Header

There are directives in the script header that are applicable to the entire script. The header in this instance has four directives:

%dw 2.0: Denotes the DataWeave version to be utilized. If this directive is absent, the version that is used by default is 2.0.

The output’s MIME type is shown by the output application/json. A JSON object is the result in this instance.

bring in * from dw::util::Ideals. To interact with values, this imports all of the utility functions from the dw::util::Values module. The default function from this module is utilized in this script.

var res2 is equivalent to {…}: This defines a JSON object-storing variable named res2. This item includes each book’s pricing and author details. Anywhere throughout the script, the variable can be used.  

Body

 The script’s body houses the expression that creates the output structure. The body in this instance consists of a single expression. It iterates over the response array in the input payload. The map function applies a transformation to each element. The lambda function that defines the transformation requires two parameters: item and index. The item parameter represents the currently selected element in the array. The index parameter indicates its zero-based position.

To change the item object, the lambda function makes use of the update operator. The update operator receives a list of cases that define how to update certain object components. There is just one instance in this situation:

case.s -> $ map(…): This case modifies the value of the item object by matching the s key in the object. The s key holds an array of book objects. Each book object is transformed by iterating through the array using the map method. One more lambda function, defined for the transformation, accepts a single parameter, $. The book object that is currently in the array is indicated by the $ argument.  

The book object and another object containing the price key are combined by the lambda function using the ++ operator. The book object’s index is in the s array. It is used to retrieve the values for these keys from the res2 variable. If the res2 variable lacks the appropriate keys, the default function is called. The default function is also called if the index is out of limits. The merge operation is unaffected in that scenario as the default function returns an empty object.  

Conclusion

You learn how to use DataWeave in this tutorial. You converted a JSON input with book-related information into another JSON output. The new output has the price of each book added. Additionally, you gained knowledge about DataWeave’s update operator, functions, imports, and variables. These ideas and methods help you enhance the complexity of data transformations in your Mule flows. They also increase their potency.

]]>
Post a Comment (0)
Previous Post Next Post