XML to JSON

Converts XML into JSON, turning elements into keys and repeated siblings into arrays. The two formats do not map onto each other cleanly, so the useful part is knowing which compromises any converter has to make.

Live demo · real result from this tool
Off keeps every value as text, which is safest for IDs and version numbers.
Indent

JSON

{  "catalog": {    "book": [      {        "author": "Gambardella, Matthew",        "title": "XML Developer's Guide",        "price": {          "#text": "44.95",          "@currency": "USD"        },        "@id": "bk101",        "@lang": "en"      },      {        "author": "Ralls, Kim",        "title": "Midnight Rain",        "price": {          "#text": "5.95",          "@currency": "USD"        },        "@id": "bk102"
      }
    ]
  }
}

An element that appears once becomes an object, not a one-item array — check repeated elements before writing code against this shape. Comments and processing instructions are dropped.

Processed on your device — nothing is uploaded.

How to use XML to JSON

  1. Paste your XML.
  2. The JSON equivalent appears alongside it.
  3. Check how repeated elements and attributes were mapped before writing code against the shape.
  4. Attributes and text content need distinct conventions in JSON; check the output shape rather than assuming.
  5. Comments, processing instructions and namespace declarations do not survive the conversion.
  6. An element that appears once produces an object; the same element repeated produces an array.

XML carries things JSON has no slot for

An XML element can have attributes as well as child elements; JSON objects have only keys. XML preserves the order of mixed content and distinguishes an empty element from an absent one; JSON does neither. XML has namespaces, comments, processing instructions and CDATA sections. Every XML-to-JSON converter therefore invents conventions — typically prefixing attributes with @ and putting text content under a key such as #text — and no two converters agree on them. Round-tripping XML through JSON and back is not reliable.

The single-element array problem

This is the one that breaks production code. Repeated child elements become a JSON array, so three <item> elements give you a list. But a document with exactly one <item> gives an object, not an array of one — there is nothing in the XML to say the element repeats. Code written against a multi-item sample then crashes on a single-item response. Either normalise with a schema that declares which elements are collections, or write consuming code that accepts both shapes.

Types are your problem, not XML’s

Everything in XML is text. <age>28</age> is the string “28” unless a schema says otherwise, so a converter either leaves everything as strings — safe, and annoying — or guesses, which reintroduces the problem of version numbers and leading zeros becoming numbers. When the output feeds code you control, prefer strings and convert explicitly at the point of use.

Frequently asked questions

How are XML elements converted?
XML elements become JSON objects. Repeated elements become arrays. Text content and attributes are preserved in the structure.
What happens to XML attributes?
They are mapped into the object alongside child elements, conventionally with a prefix such as @ to keep them from colliding with element names. Since conventions differ between tools, inspect the output rather than assuming a particular one.
Can it handle complex XML?
Yes, the converter handles nested elements, mixed content, and attributes, but very complex XML may need specialized processing.
Can I convert the JSON back to the original XML?
Not faithfully. Attribute conventions, element order, namespaces, comments and the empty-versus-absent distinction are lost or approximated, so the XML you get back will be structurally similar rather than identical. Treat the conversion as one-way.
Why did my list become an object?
Because it contained exactly one element. XML cannot express “this is a collection that happens to have one member” without a schema, so converters produce an object. Write consuming code that handles both, or normalise the shape after conversion.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close