JSON to XML conversion fails due to "undeclared prefix" Error

Hello,

im trying to convert JSON to XML and it fails with the following error:

"JSONToXML[JSONToXML]: Execution failed due to reason: undeclared prefix: soapenv"

Im using the following policy:

<JSONToXML name='JSONToXML'>
	<Options>
		<NamespaceSeparator>_</NamespaceSeparator>
		<DefaultNamespaceNodeName/>
		<NamespaceBlockName>#namespaceblock</NamespaceBlockName>
		<NullValue>I_AM_NULL</NullValue>
	</Options>
	<OutputVariable>request.content</OutputVariable>
	<Source>requestJSON</Source>
</JSONToXML>

The JSON in requestJSON variable looks like this:

{
   "soapenv_Envelope":{
      "#namespaceblock":{
         "xmlns_soapenv":"http://schemas.xmlsoap.org/soap/envelope/"
      },
      "soapenv_Header":{
            
       },
       "soapenv_Body":{
		[...]
          }
       }
    }
}

Why is the error thrown? Accoring to the documentation this should work fine since the namespaces of the XML are declared in a block and the name of the attribute is declared in the policy.

Solved Solved
1 3 1,449
1 ACCEPTED SOLUTION

Try this for your source JSON:

{
  "soapenv_Envelope": {
    "#namespaceblock": {
      "soapenv":"http://schemas.xmlsoap.org/soap/envelope/"
    },
    "soapenv_Header":{
      "baz" : "buh"
    },
    "soapenv_Body":{
      "foo" : "bar"
    }
  }
}

Note: the properties within the #namespaceblock should not contain the xmlns prefix followed by the separator. Just the namespace prefix.

Later, in the rest of the blocks, you will use the namepace prefix followed by the separator.

When I use that as the source JSON, I get this output XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <baz>buh</baz>
   </soapenv:Header>
   <soapenv:Body>
      <foo>bar</foo>
   </soapenv:Body>
</soapenv:Envelope>

View solution in original post

3 REPLIES 3

Not applicable

I would suggest to do json to XML first then use javascript or assign message policy to frame properly.

Try this for your source JSON:

{
  "soapenv_Envelope": {
    "#namespaceblock": {
      "soapenv":"http://schemas.xmlsoap.org/soap/envelope/"
    },
    "soapenv_Header":{
      "baz" : "buh"
    },
    "soapenv_Body":{
      "foo" : "bar"
    }
  }
}

Note: the properties within the #namespaceblock should not contain the xmlns prefix followed by the separator. Just the namespace prefix.

Later, in the rest of the blocks, you will use the namepace prefix followed by the separator.

When I use that as the source JSON, I get this output XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <baz>buh</baz>
   </soapenv:Header>
   <soapenv:Body>
      <foo>bar</foo>
   </soapenv:Body>
</soapenv:Envelope>

That solved it! Thank you very much. I just didn't see it, I was so close