JsonToXml: how to emit an XML element with attribute and text node?

Using the JsonToXml policy I want to create the following XML element:

<Root>
  <description length="short">Bla Bla Bla</description>
</Root>	

Creating the similar XML

<Root>
  <description length="short">
    <value>Bla Bla Bla</value>
  </description>
</Root>

is easy providing this JSON too the policy:

{
  "description": {
    "#attrs": {
      "@length": "short"
    },
    "value": "Bla Bla Bla"
  }
}

But I did not succeed in providing a JSON, that creates the element "description" with attribute "length" without child nodes, but just the single value.

Is there a way to achieve this?

Solved Solved
0 3 265
1 ACCEPTED SOLUTION

You need TextNodeName under Options. I was able to get the output you want with this json:

{
 "Root" : {
   "description": {
     "#attrs": {
       "@length": "short"
     },
     "text": "Bla Bla Bla"
    }
  }
}

And this policy configuration

<JSONToXML name='JSONToXML-1'>
  <Source>contrivedMessage</Source>
  <OutputVariable>response</OutputVariable>
   <Options>
     <TextNodeName>text</TextNodeName>
     <AttributeBlockName>#attrs</AttributeBlockName>
   </Options>
</JSONToXML>

output:

<Root><description length="short">Bla Bla Bla</description></Root>

View solution in original post

3 REPLIES 3

You need TextNodeName under Options. I was able to get the output you want with this json:

{
 "Root" : {
   "description": {
     "#attrs": {
       "@length": "short"
     },
     "text": "Bla Bla Bla"
    }
  }
}

And this policy configuration

<JSONToXML name='JSONToXML-1'>
  <Source>contrivedMessage</Source>
  <OutputVariable>response</OutputVariable>
   <Options>
     <TextNodeName>text</TextNodeName>
     <AttributeBlockName>#attrs</AttributeBlockName>
   </Options>
</JSONToXML>

output:

<Root><description length="short">Bla Bla Bla</description></Root>

ps: an option to ask for indenting in the XML output is coming soon!

Thanks!