XML to JSON policy

I have XML data which is converted to JSON using XML to JSON policy. The policy code is:

 

<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON">
  <DisplayName>XML to JSON</DisplayName>
  <Properties/>
  <Format>yahoo</Format>
  <OutputVariable>response</OutputVariable>
  <Source>response</Source>
</XMLToJSON>

 

My XML has language information for an employee. If employee knows multiple language the JSON output for language field is an array. Whereas if an employee knows single language JSON output is string. I want JSON output to be array always.

If you see below for emp#123 language data in enclosed in [] and for emp#567 it is just a string

XML data

 

<?xml version="1.0" encoding="UTF-8"?>
<AboutMe>
  <Emplid>123</Emplid>
  <Languages>German(Speaking)</Languages>
  <Languages>German(Writing)</Languages>
  <Languages>German(Reading)</Languages>
  <Languages>French(Reading)</Languages>
  <Languages>French(Speaking)</Languages>
  <Languages>French(Writing)</Languages>
  <Languages>English(Reading)</Languages>
  <Languages>English(Writing)</Languages>
  <Languages>English(Speaking)</Languages>
</AboutMe>

 

and

 

<?xml version="1.0" encoding="UTF-8"?>
<AboutMe>
  <Emplid>567</Emplid>
  <Languages>English(Speaking)</Languages>
</AboutMe>

 

JSON Output

 

{
  "AboutMe": {
    "Emplid": 123,
    "Languages": [
      "German(Speaking)",
      "German(Writing)",
      "German(Reading)",
      "French(Reading)",
      "French(Speaking)",
      "French(Writing)",
      "English(Reading)",
      "English(Writing)",
      "English(Speaking)"
    ]
  }
}

 

and

 

{
  "AboutMe": {
    "Emplid": 567,
    "Languages": "English(Speaking)"
  }
}

 

0 1 67
1 REPLY 1

You want to use the TreatAsArray element in the XMLToJSON policy.  It looks like this: 

 

<XMLToJSON name='XMLToJSON-1'>
  <Source>exampleMessage</Source>
  <OutputVariable>transformed.content</OutputVariable>
  <Options>
    <TreatAsArray>
      <Path>AboutMe/Languages</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

 

Find the relevant documentation here.

When I try this, using this XML input:

<AboutMe>
  <Emplid>567</Emplid>
  <Languages>German(Speaking)</Languages>
</AboutMe>

...I get this output: 

{
  "AboutMe": {
    "Emplid": "567",
    "Languages": [
      "German(Speaking)"
    ]
  }
}