json2xml error undeclared prefix: cbc

hello

i have this problem in the policy imput

json

{
    "Invoice": {
        "cbc:CustomizationID": "25",
    }
       
}

exceution error

{ 
    "fault": 
    { 
        "faultstring": "JSONToXML[JTXInvoiceUbl]: Execution failed due to reason: undeclared prefix: cbc", 
        "detail": { 
            "errorcode": "steps.json2xml.ExecutionFailed"
         } 
    } 
}

expected result

<?xml version="1.0" encoding="UTF-8"?>
<Invoice >
    <cbc:CustomizationID>05</cbc:CustomizationID>
    
</Invoice>



1 1 769
1 REPLY 1

The JSONToXML policy will never emit something like this:

<Invoice >
    <cbc:CustomizationID>25</cbc:CustomizationID>
</Invoice>

The above is not well-formed XML. In other words, it's not XML.

It uses a prefix, "cbc" which is undeclared. According to XML Namespace rules, the document must declare all prefixes it uses. This, for example, is valid:

<Invoice xmlns:cbc="https://my.cbc.ns/whatever">
    <cbc:CustomizationID>25</cbc:CustomizationID>
</Invoice>

Supposing you are starting with this JSON:

{
  "Invoice": {
    "cbc:CustomizationID": "25"
  }
}

To get the "well-formed" document above, you would need to do 2 things:

  1. use this JavaScript to add the necessary elements to the original JSON:
        var c = JSON.parse(context.getVariable('myRequest.content'));
        if (c && c.Invoice) {
          c.Invoice['#ns'] = {
            'cbc': 'https://my.cbc.ns/whatever'
          };
          context.setVariable('myRequest.content', JSON.stringify(c,null,2));
        }<br>
    	
  2. Use this JSONToXML policy configuration:
    <JSONToXML name='JSONToXML-1'>
      <Source>myRequest</Source>
      <OutputVariable>myRequest</OutputVariable>
      <Options>
        <NamespaceBlockName>#ns</NamespaceBlockName>
        <NamespaceSeparator>:</NamespaceSeparator>
      </Options>
    </JSONToXML>
    	

The policy steps should occur in that order: JS to add the elements, then JSONToXML to xform to XML.

Please find attached a working proxy that demonstrates this.

apiproxy-json2xml-with-namespaces.zip