JSON ↔ XML

Convert between JSON and XML formats bidirectionally

Shortcuts: Ctrl+Enter run · Ctrl+L clear · Ctrl+D download

About JSON ↔ XML

This tool converts between JSON and XML in both directions. XML is still widely used in enterprise systems, SOAP APIs, configuration files (Maven, Spring, Android), and document formats. Converting between the two formats is a common task when integrating modern REST APIs with legacy systems or vice versa.

  • ✓Bidirectional conversion — JSON to XML and XML to JSON
  • ✓Produces well-formed, properly indented XML output
  • ✓Handles JSON arrays by repeating XML elements
  • ✓Preserves attribute and text node semantics when converting XML to JSON

Mapping Between JSON and XML

JSON and XML both describe nested data, but they are not equivalent. XML has attributes, repeated elements and mixed text content; JSON has arrays, numbers, booleans and null. Every converter therefore follows a set of mapping conventions. Knowing them lets you predict the output and avoid surprises when the result is fed into another system.

JSON → XML

  • ·The whole document is wrapped in a <root> element, because XML requires exactly one top-level element
  • ·Each object key becomes a child element with the same name
  • ·An array becomes the same element repeated once per item
  • ·Keys starting with @_ become attributes on the parent element instead of children
// Input
{ "book": { "@_id": "42", "title": "Dune", "tags": ["scifi", "classic"], "price": 9.99 } }

// Output
<root>
  <book id="42">
    <title>Dune</title>
    <tags>scifi</tags>
    <tags>classic</tags>
    <price>9.99</price>
  </book>
</root>

Keys must be valid XML element names. JSON allows any string as a key, but XML names cannot start with a digit or contain spaces, so a key like "2nd place" needs renaming before conversion.

XML → JSON

Going the other way, the same conventions apply in reverse: attributes appear as @_-prefixed keys, repeated sibling elements become an array, and an empty element becomes an empty string. Element text that looks like a number or boolean is converted to one, while attribute values stay strings.

<order id="7"><item>pen</item><item>ink</item><qty>3</qty><note/></order>

→

{ "order": { "item": ["pen", "ink"], "qty": 3, "note": "", "@_id": "7" } }

The single-item array trap

If the order above had contained only one item element, the result would be "item": "pen" — a plain string, not an array with one entry. XML has no way to say "this is a list" when there is only one element, so the converter cannot know. Code that consumes converted XML should therefore handle both a single value and an array for any element that can repeat.

Where this comes up

  • ·Integrating with SOAP services, RSS and Atom feeds, or older enterprise APIs that only speak XML
  • ·Reading configuration formats such as Maven pom.xml or Android manifests in a JSON-based toolchain
  • ·Producing XML for systems that require it from data your application already holds as JSON

Tip: Round-tripping JSON → XML → JSON is not always lossless: types, empty arrays and single-item arrays can change. Validate the final JSON against what your code expects.

Frequently Asked Questions

Ad