Altova XMLSpy 2025 Enterprise Edition

Convert XML Instance to/from JSON/YAML

Home Prev Top Next

If the active document is an XML document, this command generates a JSON or YAML document from it. If the active document is a JSON or YAML 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 information about JSON and JSON editing support in XMLSpy, see the section JSON and JSON Schema. For information about YAML, see the section YAML.

 

Sample conversions

Given below is an example of a source XML document, and, below it, the JSON and YAML documents generated from it by the Convert XML Instance to/from JSON/YAML 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>

 

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

 

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/YAML command.

 

YAML document

 

Person:

   "@first": Jim

   "@last": James

   Address:

       street: 4 New Street

       city: New York

       state: NY

       code: "10123"

   Tel:

       -   "@type": home

           $: |

           

                 123 123-1234

             

       -   "@type": office

           $: |

           

                 123 987-9876

 

 

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

 

XML to JSON/YAML conversion options

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

JSONConvertXMLInstance2JSON

The first two options define whether prefixes should be added to JSON/YAML 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/YAML property @somenode. In this way, a conflict with the node created from the XML element somenode (which is the JSON/YAML property somenode) is avoided.

 

XML instance

 

<root somenode="value">

   <somenode>content</somenode>

</root>

 

 

JSON instance

 

{

   "root": {

       "@somenode": "value",

       "somenode": "content"

   }

}

 

 

YAML 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 node has a namespace prefix, then the corresponding JSON/YAML 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 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/YAML to XML conversion options

When you click the Convert XML Instance to/from JSON/YAML command to convert a JSON or YAML instance document to an XML instance document, the Convert JSON/YAML 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 or YAML instance, and the generated XML document will be opened in a new window.

ConvertJSONYAMLInstanceToXML

Note the following points:

 

JSON/YAML 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/YAML 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.

 

© 2018-2024 Altova GmbH