Skip to content
AZ Tools

XML ↔ JSON Converter

Convert XML to JSON and back, with full attribute support, configurable attribute prefix and text key.

How to use

  1. Pick the direction. Drop in XML (or JSON) — sample data is one click away.
  2. Tune the attribute prefix and text key if you're targeting a specific schema (x2js, fast-xml-parser, custom).
  3. JSON→XML lets you set a root element name and toggle the <?xml ... ?> declaration. Copy when ready.

About this tool

Show

A bidirectional converter that keeps what most XML-to-JSON tools throw away: attributes, mixed content and repeated elements. Attributes become keys under a configurable prefix, so <book id="1"> yields {"@id": "1"}; text sitting alongside attributes goes into a #text key; repeated child elements collapse into an array. The XML side is parsed by the browser's own DOMParser, the same code path used for an application/xml response, so namespaces, entities and CDATA behave exactly as they would in your page.

The conversion is inherently lossy in one direction because the two formats disagree about what a document is. JSON has arrays; XML does not, and infers them from repetition — which means the shape of the JSON depends on the data rather than the schema. One <item> produces an object, two produce an array of objects, and code written against a sample with two items breaks on the day a customer sends one. If a consumer needs a stable shape, normalise after converting rather than trusting the input to stay plural.

Ordering is the other casualty. XML preserves the sequence of everything, including the interleaving of text and elements in mixed content like <p>Hello <b>world</b> again</p>. JSON objects have no meaningful order, so that sentence cannot be reconstructed from keys alone. For document-shaped XML — anything where the elements are inside prose rather than around data — a JSON representation is a lossy summary rather than a translation.

Namespaces carry a subtler trap. The prefix in x:title is arbitrary and local to the document; the URI it is bound to is the part that identifies the vocabulary. Flattening to JSON keeps the prefix as text in the key and drops the binding, so two files describing the same thing with different prefixes produce different JSON, and two files using the same prefix for different vocabularies produce colliding JSON. If namespaces matter to what you are building, resolve them before the conversion, not after.

Frequently asked questions

Why prefix attributes?

Because JSON doesn't natively distinguish attributes from child elements. Prefixing (with @, _, or anything you like) lets attributes survive the round trip — converting XML→JSON→XML brings them back as attributes rather than child elements. Without the convention, attributes would collide with child element names.

Does it handle mixed content (<p>hello <b>world</b>!</p>)?

Partially. Pure text becomes a string; element children with text become { #text: '...' } alongside the children. Interleaved text and elements (like hello <b>world</b>!) collapse to the joined text in the #text key, which round-trips but loses the original ordering. For documents with heavy mixed content (HTML, DocBook), a dedicated parser is better.

Why did a list with one item become an object instead of an array?

Because XML has no way to say that an element repeats: three sibling tags make a list and one makes a single value, and nothing in the document distinguishes a one-item list from a scalar. A converter has to guess, so a feed that usually returns several entries silently changes shape on the day it returns one. Handle both forms on the receiving side, or convert against a schema that states which elements are repeatable.

What is lost converting XML to JSON and back?

More than most people expect. Namespaces flatten into prefixed names, comments and processing instructions disappear, the distinction between an attribute and a child element survives only by convention, and CDATA becomes ordinary text. Document order is kept within an array but not between differently named siblings. Use the conversion to read and transform data, not as a storage format for XML you have to reproduce faithfully.

Related tools