Using the JSONToXML policy to produce xml from json, how can I selectively add a namespace prefix?

im using json to xml  policy to convert json: i would like to add a prefix "req": to nodes  like in the impec, where can i add this prefix in the config of the xml policy which looks like this: 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONToXML async="false" continueOnError="false" enabled="true" name="JTX-json-to-xml">
<DisplayName>JTX-json-to-xml</DisplayName>
<Properties/>
<Options>
<NullValue>NULL</NullValue>
<NamespaceBlockName>req:#namespaces</NamespaceBlockName>
<DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
<NamespaceSeparator>:</NamespaceSeparator>
<TextNodeName>#text</TextNodeName>
<AttributeBlockName>#attrs</AttributeBlockName>
<AttributePrefix>@</AttributePrefix>
<InvalidCharsReplacement>_</InvalidCharsReplacement>
<ArrayRootElementName>Array</ArrayRootElementName>
<ArrayItemElementName>Item</ArrayItemElementName>
</Options>
<OutputVariable>request</OutputVariable>
<Source>request</Source>
</JSONToXML>

json sent looks like: 

{
  "IthZcom": {
    "IthTec": {
      "IthTecTypmsg": "Q",
      "IthTecNumvrs": 1,
      "IthTecJavpgm": "SOAP",
      "IthTecMvspgm": "IMWPAQ0",
      "IthTecIdfreq": "TU PCBF",
      "IthTecTypgtw": "W",
      "IthTecIdfptn": "CB",
      "IthTecIdfcnl": "E",
.
.
.
}

 

alaedine_0-1656926722049.png

thank you in advance,

 

Solved Solved
0 1 148
1 ACCEPTED SOLUTION

Thanks for including the policy configuration for the JSONToXML policy. I don't quite understand your phrase "like in the impec". But I understand you want to prepend a req: prefix to some subset of nodes in the resulting XML document. In XML parlance, req: is an example of a namespace prefix.

The JSONToXML policy cannot be used to perform that work. There is no configuration you can apply to that policy that would result in some of the nodes getting remapped to different node names (eg, nodes with a namespace prefix). The way to do what you want is to use a separate, distinct policy.

You would place the additional policy either immediately before, or immediately after, the JSONToXML policy. There are two options available to you:

  • Use a JavaScript policy to apply a prefix to each node name as desired, then use JSONToXML to generate the desired XML document.
  • use JSONToXML and then use an XSLT policy to prepend the prefix to selected nodes. (hints here)

Which one you choose depends on your preferences. Are you comfortable writing and maintaining XSL? Would you rather code in JavaScript? Either will work, and the performance will be similar. Which option are you more comfortable with? 

View solution in original post

1 REPLY 1

Thanks for including the policy configuration for the JSONToXML policy. I don't quite understand your phrase "like in the impec". But I understand you want to prepend a req: prefix to some subset of nodes in the resulting XML document. In XML parlance, req: is an example of a namespace prefix.

The JSONToXML policy cannot be used to perform that work. There is no configuration you can apply to that policy that would result in some of the nodes getting remapped to different node names (eg, nodes with a namespace prefix). The way to do what you want is to use a separate, distinct policy.

You would place the additional policy either immediately before, or immediately after, the JSONToXML policy. There are two options available to you:

  • Use a JavaScript policy to apply a prefix to each node name as desired, then use JSONToXML to generate the desired XML document.
  • use JSONToXML and then use an XSLT policy to prepend the prefix to selected nodes. (hints here)

Which one you choose depends on your preferences. Are you comfortable writing and maintaining XSL? Would you rather code in JavaScript? Either will work, and the performance will be similar. Which option are you more comfortable with?