How do I remove the prologue (declaration) coming out of JSONToXML Transform?

Not applicable

My output from JSON to XML is coming out with <?xml version="1.0" encoding="UTF-8"?>. How can I prevent this from happening? I can write a dummy XSLT with omit-xml-declaration="yes". It may or may not work, but I do not want to run a XSLT just for this.

The purpose is - I am creating a child nodeset with this transform. I want to include this child node as-is in my next assign message step to create a SOAP message.

Solved Solved
1 4 2,136
1 ACCEPTED SOLUTION

Former Community Member
Not applicable

Hi @Nilesh Kumar, the JSON to XML policy by default will output the prolog in the generated XML. An alternative approach is to consider adding an Extract Variable policy right after the JSON to XML Policy & extract the root element minus the prolog.

For eg if the XML document is set in a variable name xmlResponse like this:

<?xml version="1.0" encoding="UTF-8"?>
<Name>
  <First>Joe</First>
  <Last>Smith</Last>
</Name

then an Extract Variable policy like this:

<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables">
    <DisplayName>Extract Variables</DisplayName>
    <Properties/>
    <Source clearPayload="false">xmlResponse</Source>
    <XMLPayload stopPayloadProcessing="false">
        <Namespaces/>
        <Variable name="newXMLResponse" type="nodeset">
            <XPath>//Name</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

will result in variable newXMLResponse like:

<Name>
  <First>Joe</First>
  <Last>Smith</Last>
</Name

Hope this helps.

View solution in original post

4 REPLIES 4

Former Community Member
Not applicable

Hi @Nilesh Kumar, the JSON to XML policy by default will output the prolog in the generated XML. An alternative approach is to consider adding an Extract Variable policy right after the JSON to XML Policy & extract the root element minus the prolog.

For eg if the XML document is set in a variable name xmlResponse like this:

<?xml version="1.0" encoding="UTF-8"?>
<Name>
  <First>Joe</First>
  <Last>Smith</Last>
</Name

then an Extract Variable policy like this:

<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables">
    <DisplayName>Extract Variables</DisplayName>
    <Properties/>
    <Source clearPayload="false">xmlResponse</Source>
    <XMLPayload stopPayloadProcessing="false">
        <Namespaces/>
        <Variable name="newXMLResponse" type="nodeset">
            <XPath>//Name</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

will result in variable newXMLResponse like:

<Name>
  <First>Joe</First>
  <Last>Smith</Last>
</Name

Hope this helps.

FYI, The desired behavior - omit the XML Declaration - is covered in a feature request, ref: b/65142394.

Well, I tried this <OmitXmlDeclaration>true</OmitXmlDeclaration> property but it doesn't seems working.

Show me. Show me the complete configuration. Also, what version of Apigee are you using? 

Here's an example that works for me on Apigee Edge (hosted).  It will work the same on Apigee X and hybrid. 

Suppose the inbound JSON is 

{
  "Order": {
    "Id": "00001282",
    "ShippingGroup": {
      "Id": "sh00005502",
      "LineItems": [
        {
          "LineItem": {
            "Id": "plida8055335850643b1aa9331ee3",
            "Quantity": "5"
          }
        },
        {
          "LineItem": {
            "Id": "plia762acad1a4e500029c72590dd",
            "Quantity": "5"
          }
        }
      ]
    }
  }
}

I have two distinct JSONToXML policies. One uses OmitXmlDeclaration and Indent like this:

<JSONToXML name='J2X-1'>
  <Source>contrivedRequest</Source>
  <OutputVariable>contrivedRequest</OutputVariable>
  <Options>
    <Indent>true</Indent>
    <OmitXmlDeclaration>true</OmitXmlDeclaration>
  </Options>
</JSONToXML>

And the other without those options, like this: 

<JSONToXML name='J2X-2'>
  <Source>contrivedRequest</Source>
  <OutputVariable>contrivedRequest</OutputVariable>
  <Options>
  </Options>
</JSONToXML>

The first policy will result in this output: 

<Order>
   <Id>00001282</Id>
   <ShippingGroup>
      <Id>sh00005502</Id>
      <LineItems>
         <LineItem>
            <Id>plida8055335850643b1aa9331ee3</Id>
            <Quantity>5</Quantity>
         </LineItem>
      </LineItems>
      <LineItems>
         <LineItem>
            <Id>plia762acad1a4e500029c72590dd</Id>
            <Quantity>5</Quantity>
         </LineItem>
      </LineItems>
   </ShippingGroup>
</Order>

The second policy will result in this output: 

<?xml version="1.0" encoding="UTF-8"?><Order><Id>00001282</Id><ShippingGroup><Id>sh00005502</Id><LineItems><LineItem><Id>plida8055335850643b1aa9331ee3</Id><Quantity>5</Quantity></LineItem></LineItems><LineItems><LineItem><Id>plia762acad1a4e500029c72590dd</Id><Quantity>5</Quantity></LineItem></LineItems></ShippingGroup></Order>

You can find the proxy bundle I used, attached here.