Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

TreatAsArray option not working

Not applicable

Hi,

I am having issue using TreatAsArray option in XMLtoJSON policy. It doesn't return as an array if there is only 1 customer. If more than one then it returns as array. Below is the sample i have provided with 1 customer.

sample XML o/p

<Customers>
<CustomerCount>1</CustomerCount>
  <Customer>
    <Age>40</Age>
    <Gender>M</Gender>
    <Location>IBM</Location>
  </Customer>
</Customers>

Xml to Json in Apigee

{
  "Customers": {
    "CustomerCount": 1,
    "Customer": {
      "Age": 40,
      "Gender": "M",
      "Location": "IBM"
    }
  }
}

Expected Result:

{
  "Customers": {
    "CustomerCount": 1,
    "Customer": [
      {
      "Age": 40,
      "Gender": "M",
      "Location": "IBM"
      }
    ]
  }
}

XML to Json Policy code

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-2">
    <DisplayName>XML to JSON-2</DisplayName>
    <Source>response</Source>
    <OutputVariable>response</OutputVariable>
    <Options>
        <RecognizeNumber>true</RecognizeNumber>
        <RecognizeNull>true</RecognizeNull>
        <RecognizeBoolean>true</RecognizeBoolean>
        <TreatAsArray>
            <Path>Customers/CustomerCount/Customer</Path>
        </TreatAsArray>
    </Options>
</XMLToJSON>
Solved Solved
1 4 270
1 ACCEPTED SOLUTION

Hi @Nalini - Please change your policy configuration to (looks like the Path in your config is incorrect) -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-2">
    <DisplayName>XML to JSON-2</DisplayName>
    <Source>response</Source>
    <OutputVariable>response</OutputVariable>
    <Options>
        <RecognizeNumber>true</RecognizeNumber>
        <RecognizeNull>true</RecognizeNull>
        <RecognizeBoolean>true</RecognizeBoolean>
        <TreatAsArray>
            <Path>Customers/Customer</Path>
        </TreatAsArray>
    </Options>
</XMLToJSON>

View solution in original post

4 REPLIES 4