Convert XML Instance to/from JSON

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > Menu Commands > Convert Menu >

Convert XML Instance to/from JSON

If the active document is an XML document, this command generates a JSON document from it. If the active document is a JSON document, the command generates an XML document from it. The generated document is opened in a new window, and can then be saved to any location. Conversion options are described below. For more information about JSON and JSON editing support in XMLSpy, see the section JSON and JSON Schema.

 

Sample conversions

Given below is an example of a source XML document, and, below it, the JSON document generated by the Convert XML Instance to/from JSON command.

 

XML document

 

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

<Person first="Jim" last="James">

  <Address>

    <street>4 New Street</street>

    <city>New York</city>

    <state>NY</state>

    <code>10123</code>

  </Address>

  <Tel type="home">

      123 123-1234

  </Tel>

  <Tel type="office">

      123 987-9876

  </Tel>

</Person>

 

JSON document

 

{

 "XML": {

         "version": 1.0, 

         "encoding": "UTF-8"

 }, 

 "Person": {

         "first": "Jim", 

         "last": "James", 

         "Address": {

                 "street": "4 New Street", 

                 "city": "New York", 

                 "state": "NY", 

                 "code": 10123

         }, 

         "Tel": [ { "type": "home", 

                 "Text": "\r      123 123-1234\r  "}, { "type": "office", 

                 "Text": "\r      123 987-9876\r  "} ]

 }

}

 

 

To convert a JSON document to XML, make the JSON document active and click the Convert XML Instance to/from JSON command.

 

XML to JSON conversion options

When you click the Convert XML Instance to/from JSON command to convert an XML instance document to a JSON instance document, the Convert XML to JSON dialog (screenshot below) appears. You can select whether you wish to convert to JSON or JSON5. Then set the conversion options you want, and click OK. A JSON instance document will be generated from the XML instance, and the generated JSON document will be opened in a new window.

JSONConvertXMLInstance2JSON

The first two options define whether prefixes should be added to JSON property names so that conflicts with elements at the same level are avoided. The two listings below explain this. The XML attribute somenode has been converted to the JSON property @somenode. In this way, a conflict with the JSON property somenode (created from the XML element somenode) is avoided.

 

XML instance

 

<root somenode="value">

    <somenode>content</somenode>

</root>

 

 

JSON instance

 

{

    "root": {

        "@somenode": "value",

        "somenode": "content"

    }

}

 

 

The next options enable you to specify whether certain types of XML nodes are to be converted or not. If XML comments are included they are given the name "#". Text nodes (that typically occur in elements with mixed content) are given the name "$". If an XML nodes has a namespace prefix, then the corresponding JSON name will be created with this namespace prefix. If elements with the same name exist at the same level, they are considered to be equal components. Similarly, nodes such as comments, processing instructions, and text() at the same level are also equal components. If equal components are present at the same level, you are able to choose whether to create an array or not. The options are whether to create the array out of all such equal components, only neighboring equal components, or not to create an array at all.

 

The Use XML Schema type info option enables conversions to be made on the basis of the XML Schema type of the source node. For example, if a node is defined in the schema as being of type xs:string, then the target JSON object's property will be of JSON type string and will be enclosed in quotes. This is useful if, for example, a number is stored as a string in the source XML node and the conversion to JSON must also be faithful in terms of type.

 

JSON to XML conversion options

When you click the Convert XML Instance to/from JSON command to convert a JSON instance document to an XML instance document, the Convert JSON to XML dialog (screenshot below) appears. Set the conversion options you want, and click OK. An XML instance document will be generated from the JSON instance, and the generated XML document will be opened in a new window.

JSONConvertJSONInstance2XML

Note the following points:

 

JSON object properties are converted to XML elements. The first options in the dialog enable you to chose whether some types of properties are created or not.
Encode colons in property names: If selected, colons in JSON names are encoded and not created as colons. If not selected, colons are left as is.
Keep JSON value type information: If selected, a property's JSON type information is created as an attribute-value pair of the corresponding element.
Create container element for every JSON array: The container element in the XML document will be given the name of the JSON array object. The items of the JSON array are created as XML elements within this container. Each is given the name you specify in the Array item element name text box.

 


© 2019 Altova GmbH