In this Article, we will see how we can optimize and allow Json data to accept and transform duplicate elements. In real-time projects optimizing data really matters when we talk about tuning performance. Either you say in terms of logging data or in terms of processing data every byte matters.
<user>
<personal_information>
<first_name version="V1">Manish</first_name>
<first_name version="V1">Mak</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>
%dw 2.0
output application/json duplicateKeyAsArray=true
---
payload
{
"users": {
"user": {
"personal_information": {
"first_name": [
"Manish",
"MaK"
],
"middle_name": "Kumar",
"last_name": "Sah"
}
}
}
}
In DataWeave, we have two properties which will hep us to optimize our application data.
- duplicateKeyAsArray: JSON data does not allow duplicate keys within same parent, so we may get exception in real-time project development scenario. With this attribute, if your dataweave finds any duplicate keys, it will create an array with all those values.
Sample XML Input:
<users><user>
<personal_information>
<first_name version="V1">Manish</first_name>
<first_name version="V1">Mak</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>
Dataweave Script:
%dw 2.0
output application/json duplicateKeyAsArray=true
---
payload
Sample Output:
{
"users": {
"user": {
"personal_information": {
"first_name": [
"Manish",
"MaK"
],
"middle_name": "Kumar",
"last_name": "Sah"
}
}
}
}
- indent: It indicates whether you want to compress your JSON data into a single line or you need JSON data for better readability.
Sample Input:
<users>
<user>
<personal_information>
<first_name version="V1">Manish</first_name>
<first_name version="V1">Mak</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>
%dw 2.0
output application/json duplicateKeyAsArray=true, indent=false
---
payload
{"users": {"user": {"personal_information": {"first_name": ["Manish","MaK"],"middle_name": "Kumar","last_name": "Sah"}}}}
Happy Learning :)
<users>
<user>
<personal_information>
<first_name version="V1">Manish</first_name>
<first_name version="V1">Mak</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>
Dataweave Script:
%dw 2.0
output application/json duplicateKeyAsArray=true, indent=false
---
payload
Sample Output:
{"users": {"user": {"personal_information": {"first_name": ["Manish","MaK"],"middle_name": "Kumar","last_name": "Sah"}}}}
Happy Learning :)
No comments:
Post a Comment