XML ↔ JSON Converter

Convert between XML and JSON instantly, in either direction

Follows the common xml-js convention: attributes become "@name" keys, text content becomes "#text" (or the bare string, for an element with no attributes or children), and repeated child tags become an array. The JSON's top-level object always has exactly one key — the root element's tag name — so converting back and forth round-trips cleanly.

About this tool

The ToolNinja XML ↔ JSON Converter converts between the two formats instantly, in either direction, following the widely-used xml-js convention: XML attributes become "@name" JSON keys, direct text content becomes a "#text" key (or the bare string value, for a leaf element with no attributes or children), and repeated child element names collapse into a JSON array automatically. The JSON side always has exactly one top-level key — the root element's tag name — which is what makes the conversion round-trip cleanly in both directions rather than losing structure. This is the same fundamental challenge every XML-to-JSON tool has to solve: XML has no native concept of 'this is a list,' so the converter has to infer array-vs-single-value from whether a tag name repeats among its siblings. Everything runs 100% in your browser using the native DOMParser — your data never leaves your machine.

When to use it

  • →Converting a legacy XML API response into JSON for use in a modern JS/TS codebase
  • →Converting a JSON config or test fixture into XML for a system that only accepts XML input
  • →Inspecting a SOAP or RSS/Atom XML payload's structure more easily in JSON form
  • →Understanding how attributes, text content, and repeated tags map between the two formats

Tips

  • ◆An XML element with only text and no attributes or children converts to a plain JSON string — the "#text" wrapper only appears when an element also has attributes or child elements mixed in with its text.
  • ◆Two or more sibling elements with the same tag name automatically become a JSON array; a single occurrence stays a plain object, not a one-item array.
  • ◆Converting JSON back to XML requires the top-level object to have exactly one key (the root element's tag name) — if you're hand-editing the JSON side, keep that invariant or the conversion back will show a clear error.

Frequently asked questions

Why does my JSON have a top-level object with just one key instead of being the data directly?

XML documents always have exactly one root element — there's no such thing as a bare XML document without one. Mirroring that with a single top-level JSON key (the root tag's name) is what keeps the conversion reversible; dropping it would mean the JSON → XML direction wouldn't know what to name the root element.

What happens to XML comments or processing instructions?

They're not represented in the JSON output — this converter focuses on elements, attributes, and text content, which is what the overwhelming majority of real XML-to-JSON use cases actually need. If you need comments preserved, this isn't the right tool for that specific case.

How does the converter decide something should be a JSON array?

Purely by counting: if two or more child elements under the same parent share a tag name, they become a JSON array under that key. A tag name that appears only once stays a plain object (or string), even if your application logic conceptually treats it as a list — XML itself doesn't distinguish 'a list with one item' from 'a single value,' so neither can this conversion.

Related tools

🥷 ToolNinja