Option to remove root element in json : XML to JSON policy

Not applicable

While converting XML to JSON,JSON object always gets the root element.

For eg:

{ "address": { "street": "XYZ", "City": "ABC" } }

But mostly the consumer dont want the Root element in JSON object.

Is there any Option available in XML to JSON policy to remove this?

When a JSON to XML policy is executed,we can have below option element to alter the Root element.

<Options> <ObjectRootElementName>address</ObjectRootElementName> </Options>

But similar way,do we have any for XML to JSON to remove or change the root node?

0 8 1,430
8 REPLIES 8

You can use XSLT to map one XML doc to another. This would work to "remove" the root element before transforming to JSON.

After the XMLToJSON policy, you can use a JS callout to remap properties, if you like.

There is a new feature coming soon that will enable you to strip the root element during the conversion. (APIRT-2578)

Thanks @Dino

Ya..I have tried JS option.But I wanted to know if any specific option there as there is similar one for JSON to XML policy.

Good to know that there is a feature coming up soon 🙂

Hi, I do have the same issue where in I need to remove the root element name. Is there a way we can do it with XML to JSON conversion policy. Is the future available now in XML to JSON conversion policy ?

The ticket APIRT-2578 has been resolved. I don't know if the option is available right now in YOUR organization. Can you please test it?

Here's the syntax:

<XMLToJSON ... > 
  ... 
  <Options> 
    ... 
    <!-- this is new --> 
    <StripLevels>3</StripLevels>
  </Options> 
</XMLToJSON> 

Hi @DChiesa,

I was facing same issue here. Is this future available now?

 

Thanks @dknezic 

can we use same thing for  for Json to XML conversion as well? 

It didn't work for me while converting from Json to XML. need to try some other way using JS only?

As far as I know, there is no similar feature in the JSONToXML policy, for stripping elements as you convert. 

The XMLToJSON policy has that feature specifically to handle SOAP-to-JSON conversions. In many cases, the API Proxy can neglect, or "wants to" ignore, the SOAP envelope and the SOAP body. The StripLevels feature allows you to do that. 

The JSONToXML policy doesn't have that same requirement.  

Maybe you could explain what you really want to do, and someone might be able to advise.