XML to JSON with single element

We are having SOAP response(XML with namespaces) and we used XML to JSON policy for conversion and what we observed is we are having issue with single element as it is not converting to array & instead it is converting to object.

Followed below & applied as per instructions..

https://community.apigee.com/articles/33374/new-edge-minifeature-the-treatasarray-option-in-th.html

We applied policy as below for xml & <ns2:ced> & <ns2:efg> are arrays(unbounded in xml)..

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
 <soap:Body>
<ns2:abc>  
<ns2:ced a="1" b="2" c="3" d="4">  
<ns2:efg>test</ns2:efg>  
</ns2:ced>
</ns2:abc>
</soap:Body></soap:Envelope>

XML to JSON policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-JSON-Response">  
<DisplayName>XML-JSON-Response</DisplayName> 
 <Properties/>  
<Options> 
 <RecognizeNull>true</RecognizeNull> 
 <TextNodeName>#text</TextNodeName> 
 <AttributePrefix>@</AttributePrefix> 

 <TreatAsArray>  <!-- new element --> 
 <Path unwrap="true">/soap:Envelope/soap:Body/ns2:abc/ns2:ced</Path> 
</TreatAsArray>
<TreatAsArray>  <!-- new element --> 
 <Path unwrap="true">/soap:Envelope/soap:Body/ns2:abc/ns2:ced/ns2:efg</Path> 
</TreatAsArray>    
</Options> 
 <OutputVariable>response.content</OutputVariable> 
 <Source>response.content</Source>
</XMLToJSON>

How does the path works with namespaces to treat as array?

Do we need to declare the namespaces to navigate the path?

Applied above but doesn't make any difference.Please suggest.

Thanks.

0 2 1,073
2 REPLIES 2

Try it without the leading slash and without namespaces.

<XMLToJSON name='XMLToJSON-1'>
  <Source>contrivedMessage</Source>
  <OutputVariable>transformedContent</OutputVariable>
  <Options>
    <TreatAsArray>
      <Path unwrap='false'>Envelope/Body/abc/ced</Path>
      <Path unwrap='false'>Envelope/Body/abc/ced/efg</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

I get this as a result:

{
    "Envelope": {
        "Body": {
            "abc": {
                "ced": [
                    {
                        "a": "1",
                        "b": "2",
                        "c": "3",
                        "d": "4",
                        "efg": [
                            "test"
                        ]
                    }
                ]
            }
        }
    }
}

Which I think is as expected. The Path is neither a JSONPath nor an xpath. It's simply the hierarchy of the element that you want to treat as an array. Think of it as a path on the OUTPUT, after transform to JSON (and after stripping of namespaces).

I was able to do it and removed the thread. Thanks dino for the clarification.