XML2JSON - ignore root element

Maybe this is more of a feature request.

Could we have XML2JSON policy ignore the root element so that the output is more consistent with JSON structures (JSON doesn't require a root element)?

I know its doable with JS policies but my JSON payload has some special characters that causes JS to fail when I perform JSON.parse on it.

	example: <root><a>1</a><a>2</a></root>

desired output:

[ {"a" : 1}, {"a: 2}]
Solved Solved
0 3 979
1 ACCEPTED SOLUTION

with the simple example you've given the approach I would use is an XMLToJSOn policy coupled with a JavaScript callout to fix this up just the way I Want them.

You didn't explain what "some special characters" means. I've never heard of that. Maybe you could explain with an example that exhibits the failure in JS that you mentioned. For sure, this JSON

[ {"a" : 1}, {"a: 2}]

...doesn't appear to have "special characters" and processing it will not produce an error.

likewise for any of these:

{"root":[{"a":["1","2"]}]}
[{"a":["1","2"]}]
["1","2"]

Edit - here's what I tried, it worked for me. The inbound message looks like this:

<root>
  <desc>INSTA® P8096 INSTA 0.7 MIL SILVER CENTER FOLD 96" 13200 SQ FT STANDARD SAC PRINT</desc>
  <desc>desc 2</desc>
</root>

The flow looks like this:

    ...
        <Step>
          <Name>XMLToJSON-1</Name>
        </Step>
        <Step>
          <Name>JS-PostProcess</Name>
        </Step>
    ...

The XMLToJSON looks like this:

<XMLToJSON name='XMLToJSON-1'>
  <Source>myMessage</Source>
  <OutputVariable>jsonOut</OutputVariable>
  <Options/>
 </XMLToJSON>

The JS-PostPRocess looks like this:

<Javascript name='JS-PostProcess' timeLimit='200' >
  <Source>
    var c = JSON.parse(context.getVariable('jsonOut'));
    var r = c.root.desc.map(function(element) {
      return { desc: element };
    });
    context.setVariable('jsonOutModified', JSON.stringify(r,null,2));
  </Source>
</Javascript>

The output (jsonOutModified) is like this:

[
  {
    "desc": "INSTA® P8096 INSTA 0.7 MIL SILVER CENTER FOLD 96\" 13200 SQ FT STANDARD SAC PRINT"
  },
  {
    "desc": "desc 2"
  }
]

All of this looks valid and normal and correct.

View solution in original post

3 REPLIES 3

with the simple example you've given the approach I would use is an XMLToJSOn policy coupled with a JavaScript callout to fix this up just the way I Want them.

You didn't explain what "some special characters" means. I've never heard of that. Maybe you could explain with an example that exhibits the failure in JS that you mentioned. For sure, this JSON

[ {"a" : 1}, {"a: 2}]

...doesn't appear to have "special characters" and processing it will not produce an error.

likewise for any of these:

{"root":[{"a":["1","2"]}]}
[{"a":["1","2"]}]
["1","2"]

Edit - here's what I tried, it worked for me. The inbound message looks like this:

<root>
  <desc>INSTA® P8096 INSTA 0.7 MIL SILVER CENTER FOLD 96" 13200 SQ FT STANDARD SAC PRINT</desc>
  <desc>desc 2</desc>
</root>

The flow looks like this:

    ...
        <Step>
          <Name>XMLToJSON-1</Name>
        </Step>
        <Step>
          <Name>JS-PostProcess</Name>
        </Step>
    ...

The XMLToJSON looks like this:

<XMLToJSON name='XMLToJSON-1'>
  <Source>myMessage</Source>
  <OutputVariable>jsonOut</OutputVariable>
  <Options/>
 </XMLToJSON>

The JS-PostPRocess looks like this:

<Javascript name='JS-PostProcess' timeLimit='200' >
  <Source>
    var c = JSON.parse(context.getVariable('jsonOut'));
    var r = c.root.desc.map(function(element) {
      return { desc: element };
    });
    context.setVariable('jsonOutModified', JSON.stringify(r,null,2));
  </Source>
</Javascript>

The output (jsonOutModified) is like this:

[
  {
    "desc": "INSTA® P8096 INSTA 0.7 MIL SILVER CENTER FOLD 96\" 13200 SQ FT STANDARD SAC PRINT"
  },
  {
    "desc": "desc 2"
  }
]

All of this looks valid and normal and correct.

To always treat the desc element as an array, use something like this:

<XMLToJSON name='XMLToJSON-1'>
  <Source>myMessage</Source>
  <OutputVariable>jsonOut</OutputVariable>
  <Options>
    <TreatAsArray>
      <Path>root/desc</Path>
    </TreatAsArray>
  </Options>
 </XMLToJSON>

Here is an example of one of the data nodes that caused JS policy to fail:

<desc>INSTA® P8096 INSTA 0.7 MIL SILVER CENTER FOLD 96" 13200 SQ FT STANDARD SAC PRINT</desc>

the quotes in the xml payload get converted to \" when i first run this in xmltojson and then JSON.parse blows up in JS.