I have a JSON payload that I need to convert into an XML. However that JSON payload has many fields starting with '@' which are supposed to be the XML attributes. So when I converted those into XML the '@" is being attached to each xml tag as a field(e.g. <@id>1495161</@id>). Each field with '@' is supposed to be the XML attribute and should not come as indivial XML filed/tag after the conversion.
Please see my code and output vs the expected output. Notice how all the fields with @ are transformed into xml tag. But I want all these fields with @ to transform into XML attributes.
Sample Request:
{
"trans": {
"@id": "1495144",
"@TPId": "4aec",
"@change": "0",
"@count": "1",
"@dateStamp": "2024-08-02T03:07:48",
"data": {
"@id": "d7D173564C5F14FF2AD08D621C188AF19",
"StateFlag": "0",
"DCC": "B",
"LastName": "MMNGorthy",
"Name": "Scott",
"Structure": "NonProfit",
"BusinessType": "Engineering",
"meams": "AI",
"Count": "0",
"CTier": "000",
"CTCond": "111",
"CTPl": "222",
"DO": "1",
"AuthObject": "0"
}
}
}
Code:
%dw 2.0
output application/xml
ns ns0 http://Test.Sample.Data/1.0
---
ns0#trans: payload.trans
output from my code:
<?xml version='1.0' encoding='UTF-8'?>
<ns0:trans xmlns:ns0="http://Test.Sample.Data/1.0">
<@id>1495144</@id>
<@TPId>4aec</@TPId>
<@change>0</@change>
<@count>1</@count>
<@dateStamp>2024-08-02T03:07:48</@dateStamp>
<data>
<@id>d7D173564C5F14FF2AD08D621C188AF19</@id>
<StateFlag>0</StateFlag>
<DCC>B</DCC>
<LastName>MMNGorthy</LastName>
<Name>Scott</Name>
<Structure>NonProfit</Structure>
<BusinessType>Engineering</BusinessType>
<meams>AI</meams>
<Count>0</Count>
<CTier>000</CTier>
<CTCond>111</CTCond>
<CTPl>222</CTPl>
<DO>1</DO>
<AuthObject>0</AuthObject>
</data>
</ns0:trans>
Expected output:
<?xml version='1.0' encoding='UTF-8'?>
<ns0:trans xmlns:ns0="http://Test.Sample.data/1.0" id="1495144" TPId='4aec' change='0' count='1' dateStamp="2024-08-02T03:07:48">
<data id="FF2AD08D621">
<StateFlag>0</StateFlag>
<DCC>B</DCC>
<LastName>MMNGorthy</LastName>
<Name>Scott</Name>
<Structure>NonProfit</Structure>
<BusinessType>Engineering</BusinessType>
<meams>AI</meams>
<Count>0</Count>
<CTier>000</CTier>
<CTCond>111</CTCond>
<CTPl>222</CTPl>
<DO>1</DO>
<AuthObject>0</AuthObject>
</data>
</ns0:trans>
@. Show the actual answer and the expected answer for the same input.