In project development sometimes we needed to convert the payload from xml to json. We also need to write the xml attributes after transformation.
For this purpose we can use writeAttributes=true in header section of data-weave. This attribute is available from Dataweave2.
Please find the snippets below:
<user>
<personal_information>
<first_name version="V1">Manish</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>
output application/json writeAttributes=true
---
payload
Output:
{
"users": {
"user": {
"personal_information": {
"first_name": {
"@version": "V1",
"__text": "Manish"
},
"middle_name": "Kumar",
"last_name": "Sah"
}
}
}
}
Happy Learning :)
For this purpose we can use writeAttributes=true in header section of data-weave. This attribute is available from Dataweave2.
Please find the snippets below:
Sample XML Input:
<users><user>
<personal_information>
<first_name version="V1">Manish</first_name>
<middle_name>Kumar</middle_name>
<last_name>Sah</last_name>
</personal_information>
</user>
</users>
Dataweave Script
%dw 2.0output application/json writeAttributes=true
---
payload
Output:
{
"users": {
"user": {
"personal_information": {
"first_name": {
"@version": "V1",
"__text": "Manish"
},
"middle_name": "Kumar",
"last_name": "Sah"
}
}
}
}
Happy Learning :)
Thank you! This is very helpful.
ReplyDeleteHappy to help :)
Delete