Converts between JSON and XML. This Processor wraps Newtonsoft.Json.JsonConvert.DeserializeXmlNode  and Newtonsoft.Json.JsonConvert.SerializeXmlNode  in Json.NET


Properties

Action

Type: List Input
JsonToXml - Convert JSON to XML
XmlToJson - Convert XML to JSON
Action to perform

Json

Type: Multiline Text InputOutput
If Action is JsonToXml, provide the input JSON here. If Action is XmlToJson, this property returns the converted JSON.

Xml

Type: Xml InputOutput
If Action is XmlToJson, provide the input XML here. If Action is JsonXmlTo, this property returns the converted XML.

Remarks

Use this Processor to easily convert between JSON and XML.

In XML to JSON conversion, XML attributes and namespaces will be lost.

Errors

XmlNodeConverter can only convert JSON that begins with an object.

Because XML requires a single root Node and requires a name for all nodes, the JSON passed in for conversion will need to prepared appropriately in some cases. Specifically, the entire JSON value should always be encapsulated in curly braces and all nodes should be named.

Here are some examples of invalid JSON documents:

  • "key" : "some string" should be { "key" : "some string"} 
  • [ { "key" : 1 }, { "key" : 2 } ]  should be { "root": [ { "key" : 1 }, { "key" : 2 } ] } 


Use the Formatter Node to prepare an appropriate JSON string before passing to this Node.

Emitting Arrays

XML has no equivalent to a JSON array. However, Json.NET supports a special XML attribute to force an element to render as a JSON array:

<root xmlns:json="http://james.newtonking.com/projects/json">
    <attributes json:Array="true">
    <name>Item Code</name>
    <position>1</position>
    <visible>true</visible>
    <options>
      <option>1332</option>
    </options>
  </attributes>
  <attributes json:Array="true" >
    <name>Publisher</name>
    <position>2</position>
    <visible>true</visible>
    <options>
      <option>GOODS</option>
    </options>
  </attributes>
</root>


Non-string Types

Without an XML Schema, it is not possible to infer types for XML elements. Thus, all element values are rendered as JSON strings.

To work around this problem, wrap numeric and boolean values in an escape tag, then use the Replace Node to remove the escape sequence along with the enclosing quotes.

<root>
  <field1>{{1}}</field1>
</root>


When converted to JSON, the following is returned:

{"root":{"field1":"{{1}}"}}


Use the Replace Node to replace "{{  and }}" with an empty string.

Did this answer your question?