JSON to XML and Back: Attributes, Arrays and Types Explained

How JSON maps to XML and back: @ attributes, #text, repeated or wrapped arrays, null, and type attributes that make a JSON → XML → JSON round trip lossless.

Objects become elements, @ keys become attributes

JSON has objects, arrays, strings, numbers, booleans and null. XML has elements, attributes and text. The common convention, used by the JSON to XML converter, writes each object key as a child element and each scalar as that element's text.

Keys that start with @ become attributes of their parent, and a #text key supplies the element's own text next to those attributes. With the default options, a null value becomes an empty element. Open JSON to XML, paste the example and compare the result.

{"user":{"@id":"7","name":"Ava","email":null}}

<user id="7">
  <name>Ava</name>
  <email/>
</user>

Arrays: repeated elements or a wrapper

XML has no array type, so a list is written as elements with the same name. Arrays: Repeat writes one element per item, named after the key. This is the shape most SOAP services and feeds expect.

Arrays: Wrap keeps the key as a container and names each child with its singular, so items holds item elements. Wrapping also keeps empty arrays, which produce no elements in Repeat mode.

Repeat: {"cart":{"item":["pen","ink"]}}
<cart>
  <item>pen</item>
  <item>ink</item>
</cart>

Wrap: {"cart":{"items":["pen","ink"]}}
<cart>
  <items>
    <item>pen</item>
    <item>ink</item>
  </items>
</cart>

Keep types with a lossless round trip

XML text has no data types, so the string "42" and the number 42 look the same once converted, and a one-item array looks like a single value. Converting back can therefore change your data.

Turn on Type attributes and Wrap arrays in More options. Numbers, booleans, nulls, arrays, empty strings and empty objects get a type attribute, which XML to JSON reads to restore them. The converter checks every result and shows whether the round trip is exact; Make it lossless applies both options.

{"r":{"code":"42","qty":42,"tags":["a"]}}

<r>
  <code>42</code>
  <qty type="number">42</qty>
  <tags type="array">
    <tag>a</tag>
  </tags>
</r>

Convert the XML back to JSON

Select Swap to open the XML in XML to JSON with the same document. Use the same attribute prefix and text key in both tools. Repeated elements become arrays automatically; list names under Always-array elements when a list can contain a single item.

Keep a copy of the original JSON when the XML is only an exchange format. Comments, processing instructions and the position of text between child elements have no JSON equivalent.

Reference: W3C: Extensible Markup Language (XML) 1.0.