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! Go to 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>
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>
Thanks sudheendra. I have changed the path and its working now. Why it didn't work with CustomerCount in path?
I have an extended question to this. I have a requirement with say 2 resources (end points). For these resources I will be using common XMLtoJSON policy. Each endpoint will return a unique XML response. But structure remains similar.
1st end point - xml response
<Customers> <CustomerCount>1</CustomerCount> <Customer> <Age>40</Age> <Gender>M</Gender> <Location>IBM</Location> </Customer> </Customers>
2nd end point - xml response
<NationalCustomers> <CustomerCount>1</CustomerCount> <Customer> <Age>40</Age> <Gender>M</Gender> <Location>IBM</Location> </Customer> </NationalCustomers>
Above XML the order of Customer tag remains same except the first element name is changed. So I wanted to reuse the policy for other endpoint as well rather than creating one more and giving path as NationalCustomers/Customer
Is there a way I can write path like this in XMLtoJson Policy? Some kind of relative path.
<TreatAsArray> <Path>*/Customer</Path> </TreatAsArray>
In the policy, <Path> simply the hierarchical path to the element whose values you want to put in an array. In your case it was under <Customers>.
You could either put the separate XML-to-JSON policies under those flows or use a configuration like below to handle your requirement -
<XMLToJSON 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> <Path>NationalCustomers/Customer</Path> </TreatAsArray> </Options> </XMLToJSON>
@sudheendra I have applied the changes you have suggested and it's working for all the end points. Thank you for your answer.
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |