JSON-XML Converter

This tool can be used to interconvert between JSON (JavaScript Object Notation) and XML content.

JSON to XML conversion

To convert a JSON string to XML, select the JSON to XML checkbox as shown below. The editor on the left represents the input JSON source that must be converted into XML and the editor on the right represents the output result in XML.

BadgerFish Notation Input

If the JSON string is in BadgerFish notation, then it can be converted to XML as is since the namespaces are already present in the JSON string.

The Options link can be used to configure the XML generation options:   For JSON in BadgerFish (BF) notation, the option available is “Ignore whitespace”.

Figure 9.16. JSON-XML Converter - BadgerFish notation configuration

JSON-XML Converter - BadgerFish notation configuration


Consider the following JSON source that declares the namespaces as part of the property names:

{"alice": {
    "bob": {"$": "david"},
    "charlie:edgar": {"$": "frank"},
    "@xmlns": {
        "$": "http://some-namespace",
        "charlie": "http://some-other-namespace"
    }
}}

The XML output when using BadgerFish convention is as shown below:

<alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace"><bob>david</bob><charlie:edgar>frank</charlie:edgar></alice>

If the input JSON string value has any whitespace characters like "\n" and the Ignore whitespace checkbox is enabled, then the generated XML will not have those whitespace characters present in the data.

Mapped Notation Input

For JSON in Mapped notation, the available configuration options are as shown below:

Figure 9.17. JSON-XML Converter - Mapped notation configuration

JSON-XML Converter - Mapped notation configuration


The Ignore whitespace option is as described above.

Consider the same example in mapped notation (where the prefix is present in the property, but the namespace URI is absent):

{"abc.alice":{"abc.bob":"david","xyz.edgar":"frank"}}

To convert this JSON input into XML using Mapped notation, we have the option of either specifying the namespace URI for the prefixes (abc and xyz) or leaving it unspecified. This is controlled through the Ignore XML NS? option. If this option is disabled, then the Configure NS->Prefix Mapping table is enabled and we can specify the namespace URI -> Prefix mapping.

If the URI - Prefix mapping is done as follows:

Table 9.1. Namespace URI - Prefix mapping

Namespace URIPrefix
http://some-namespaceabc
http://some-other-namespacexyz


The generated XML output is as follows with this mapping in place:

<?xml version="1.0" encoding="UTF-8"?>

<alice xmlns="http://some-namespace">
  <bob>david</bob>
  <edgar xmlns="http://some-other-namespace">frank</edgar>
</alice>

XML to JSON conversion

To convert an XML string to JSON, select the XML to JSON checkbox as shown below. The editor on the left represents the input XML source that must be converted into its equivalent JSON form and the editor on the right represents the output result in JSON.

Consider an input XML as given below that needs to be converted into JSON:

<?xml version="1.0" encoding="UTF-8"?>

<alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace">  
  <bob>david</bob>  
  <charlie:edgar>frank</charlie:edgar> 
</alice>

The above XML can be converted into JSON using either the BadgerFish notation or the Mapped notation. Clicking on the Options button opens the Conversion Options dialog as shown below:

Figure 9.18. JSON-XML converter - XML to JSON BadgerFish options

JSON-XML converter - XML to JSON BadgerFish options


BadgerFish notation output

When the BadgerFish checkbox is enabled, the only available configuration option is the Ignore whitespace checkbox, which if enabled, will ignore any whitespace characters present in the input XML document when generating the output JSON content. For the input XML above, the generated JSON string when using BadgerFish convention is as follows:

{"alice":{"@xmlns":{"$":"http:\/\/some-namespace","charlie":"http:\/\/some-other-namespace"},"bob":{"$":"david"},"charlie:edgar":{"$":"frank"}}}

Mapped notation output

When the Mapped checkbox is enabled, the configuration options are as shown below:

Figure 9.19. JSON-XML converter - XML to JSON Mapped options

JSON-XML converter - XML to JSON Mapped options


In the configuration dialog above, the Ignore whitepsace option ignores the presence of any whitespace characters in the input XML.

The Infer property types option if enabled, will cause any data such as numbers to be detected as a number and output as such instead of a string. For e.g. if an element contains the value 10, then instead of outputting the value as "10", the JSON value will be output as 10.

The Ignore XML NS? option if not enabled, allows you to declare the namespace to prefix mapping that should be applied when generating the JSON content. Note that even though the namespace URI is specified, the mapped convention does not allow for capturing the URI and only uses the prefix specified in the generated output. For the input XML above, the generated output is as follows with this option enabled:

{"alice":{"bob":"david","charlie.edgar":"frank"}}