Home XML to JSON
XML to JSON converter
Paste XML and get JSON back. XML carries things JSON has no slot for — attributes, mixed content, repeated tags — so the mapping is spelled out below rather than left to guesswork.
Attributes, text nodes and repeated elements
XML is a richer format than JSON, so every XML-to-JSON converter has to make three decisions. Converters differ, which is why the same file gives different JSON in different tools. Here they are, explicitly:
1. Attributes get an @ prefix
<order id="1001"> becomes {"@id": "1001"}. The prefix keeps
an attribute from colliding with a child element of the same name — which is legal XML and
would otherwise silently overwrite one with the other.
2. An element with only text becomes that text
<customer>Ada</customer> becomes "Ada", not
{"#text": "Ada"}. The wrapper object would be noise in the overwhelmingly
common case. When an element has both text and attributes, the text moves into a
#text key alongside them, because there is nowhere else for it to go.
3. Repeated sibling elements become an array
Two <order> elements under the same parent become a two-element array.
One <order> becomes a single object, not a one-element array — and that
asymmetry is worth knowing about, because it is the classic bug in code that consumes
converted XML. Feed it a file with one record and the consumer that expected an array
breaks. If the root element's children are all records, they are treated as the record list,
which is why the example above converts to a clean array of two orders.
Well-formedness is enforced
Parsing uses the browser's own XML parser, so a file that is not well-formed is rejected with
the reason rather than half-converted. Unclosed tags, mismatched nesting, stray
& and undeclared entities all fail here — which is the correct outcome, and
earlier than the failure you would otherwise get downstream.
Namespaces are kept as part of the element name: <ns:price> becomes the
key "ns:price". Nothing is resolved or stripped, because a prefix carries
meaning that a converter cannot safely discard.
What it is not
This is not an XSLT engine and not a schema validator. It does not read a DTD or an XSD, does not apply default attribute values from one, and does not know that a particular element should be a number. Comments, processing instructions and CDATA markers are dropped — CDATA content comes through as text.
Mixed content — text and elements interleaved inside the same tag, as in
<p>see <b>this</b> now</p> — has no faithful JSON
representation at all. The elements are kept and the loose text around them is not. If your
XML is document-shaped rather than record-shaped, JSON is the wrong target.
Common questions
How are XML attributes represented?
Each attribute becomes a key prefixed with @, so id="1001" becomes "@id": "1001". The prefix prevents an attribute from colliding with a child element that has the same name.
Why is a single repeated element not an array?
Because nothing in the XML itself says the element repeats — one <order> is indistinguishable from a list of one. This is the classic bug in code that consumes converted XML, so it is worth handling explicitly on your side.
What happens to malformed XML?
It is rejected with the parser’s reason. Parsing uses the browser’s own XML parser, so anything a browser would refuse is refused here rather than half-converted.
Are namespaces resolved?
No. A prefixed element keeps its prefix in the key, so <ns:price> becomes "ns:price". Stripping the prefix would discard meaning the converter cannot safely reconstruct.
Does it handle mixed content?
Not faithfully — nothing can. Text interleaved with elements has no JSON equivalent; the elements are kept and the loose text between them is not. Document-shaped XML is a poor fit for JSON in general.
Related
- /xml — generating well-formed XML test records instead of converting a file.
- /csv-to-json-converter — the same destination format, from a flat source.
- /json-to-csv-converter — flattening the JSON this page produces into columns.
- /api-mock-data — using the converted JSON as a fixture for a mock endpoint.
Last updated