Account

What new tool do you want?

Developer Tools

XML ↔ JSON Converter

Convert XML to JSON and JSON back to XML, keeping attributes (as @name) and repeated elements (as arrays).

How to use

  1. 1Choose the direction.
  2. 2Paste XML or JSON; attributes appear as keys starting with @.

Frequently asked questions

How are XML attributes represented?

As keys prefixed with @, e.g. <book id="7"> becomes { "book": { "@id": "7" } }. Turn attributes off to drop them.

How are repeated elements handled?

Two or more sibling elements with the same name become a JSON array.

Where does text go when an element also has attributes?

Into a #text key, e.g. <p lang="en">Hi</p> becomes { "p": { "@lang": "en", "#text": "Hi" } }.

The @ and #text conventions, and why they exist

XML has a concept JSON doesn't: an element can carry both attributes and text content at once (`<p lang="en">Hi</p>`). fast-xml-parser's convention, used here, is to prefix attribute keys with `@` and put element text under a `#text` key, so that element becomes `{ "p": { "@lang": "en", "#text": "Hi" } }` — distinct from a plain `lang` or `text` key, which could otherwise collide with a real child element of either name. Turning off 'Keep attributes' drops every `@`-prefixed key, which is useful when you only care about the data values and the attributes are metadata (IDs, namespaces) you don't need in JSON.

Repeated elements become arrays — but only if there are two or more

Two or more sibling elements with the same tag name (like the `<book>` elements in the example) collapse into a JSON array automatically. The edge case to watch for: a single occurrence of an element that could repeat (say, one `<book>` in a library that currently has only one book) comes through as a plain object, not a one-item array — so code consuming this JSON generically should handle both shapes, or you'll get a type error the day a second book is added to the source XML.

The round trip isn't perfectly symmetric

Converting JSON back to XML (via XMLBuilder with the same `@`/`#text` conventions) reproduces structurally equivalent XML, but it's worth testing both directions if you're building a pipeline — JSON has no inherent ordering guarantee between attributes and child elements the way hand-written XML might, and arbitrary JSON that wasn't originally derived from XML (e.g. deeply nested objects with no `@`/`#text` keys) will produce XML with every key as a nested element, which may not match the schema you actually need.

Worked example: XML ↔ JSON Converter

Converting `<lib><book id="7">Dune</book><book id="8">Emma</book></lib>` to JSON (with attributes on) produces a `lib` object whose `book` key is an array of two objects, each like `{ "#text": "Dune", "@id": "7" }` — the repeated `<book>` tag became an array automatically, and each book's id attribute and text content are both captured. Going the other direction, `{"a":{"@x":"1","b":["p","q"]}}` becomes `<a x="1">\n <b>p</b>\n <b>q</b>\n</a>` — the `@x` key became the attribute, and the array produced two sibling `<b>` elements.

Attributes start kept, and the return trip is indented by two spaces

Direction opens on XML to JSON, not on a guess. Keep attributes starts on. Turning it off drops attributes on the way to JSON, and on the way back it ignores names that begin with @. Broken XML is rejected with a line and a column before anything is converted. JSON that is not valid fails the same way, with a line and a column. JSON to XML is written with a two-space indent and the ends are trimmed. A single repeated-looking tag stays one object. It becomes a list only when the same name appears at least twice under one parent. The download is not forced to one extension, so I copy the result from the box.

Related tools

More: Developer Tools