XML to JSON Conversion Issues

HI,

I have a requirement where i need to convert XML 2 JSON which has numbers and i require some of them as number and some as string, currently all are converted as numbers or as string using <Options>.

please find example below

XML input:

<a:ItemResponse>
<a:Amount>1500.0400</a:Amount>
<a:xxxxxxx>3200027662909</a:xxxxxxx>
<a:RuleBreak>1</a:RuleBreak>
<a:RuleBreakData>
<a:StatusCode>
<a:Code>4</a:Code>
<a:Message>Item Decline.</a:Message>
</a:StatusCode>
</a:RuleBreakData>
<a:kkkkkkkk>685346587648678947698</a:kkkkkkkk>
</a:ItemResponse>

 

Expected outPut:

{
"ItemResponse": {
"Amount": 1500.04,
"xxxxxxx": "3200027662909",
"RuleBreak": 1,
"RuleBreakData": {
"StatusCode": {
"Code": "4",
"Message": "Item Decline."
}
},
"kkkkkkkk": "685346587648679000000"
}
}

 

Here Amount should be number and remaining shiuld be String, please let me know if there is a way to do this.

1 6 355
6 REPLIES 6

I cannot think of a way to do that with JUST the XMLToJSON policy.  But you can accomplish it with two steps. First, configure the XMLToJSON policy to NOT recognize numbers. Then each numeric text value will be treated as a String. Then, add a JS policy directly after the XMLToJSON policy, which performs a manual conversion of data types.  Basically it does this: 

var c = JSON.parse(context.getVariable('response.content'));
c.ItemResponse.Amount = Number(c.ItemResponse.Amount);
context.setVariable('response.content', JSON.stringify(c));

This works but, The challenge is sometimes 'item response'  is an array ,tried below code  but failed, 

var c = JSON.parse(context.getVariable('response.content'));
var i;
var value = c.ItemResponse.length

if (c.hasOwnProperty('Amount'))
{
for ( i = 0; i < value; i++) {
c.ItemResponse[i].Amount = Number(c.ItemResponse[i].Amount);
context.setVariable('response.content', JSON.stringify(c));
}
}
else
{
context.setVariable('response.content', JSON.stringify(c));
}

Here in above code i also want to check , existence of Amount key,  Number conversion should happen only if Amount is present in payload , else nothing should happen.


@ChaitanyaVSK wrote:

This works but, The challenge is sometimes 'item response'  is an array ,


I am guessing that  you'd prefer to have the "item response" ALWAYS be an array.   To do that, check out the TreatAsArray option in the XMLToJSON policy.  It is designed for that purpose. 

<XMLToJSON name=X2J-1'>
  <Source>response</Source>
  <OutputVariable>response.content</OutputVariable>
  <Options>
    <TreatAsArray>
      <Path unwrap='false'>itemResponse</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

And then your JS code will always work.  

I already did that,  and i tried above code , its not working. because "c.itemResponse.Amount" only works for object

Right.  You wouldn't be able to use my code, the example code I showed, if you are always dealing with an array. My code works with the original JSON you showed me.  You would have to adjust that code to use a loop - along the lines of what you showed - to handle the case in which itemResponse is an array.  It's a small adjustment.