XML to JSON Converter
runs in your browserConvert XML to JSON and JSON back to XML, keeping attributes and text nodes, with validation errors reported by line and column.
Attributes become @_name keys and an element’s own text becomes #text.
XML
JSON
about this tool
Both directions
XML to JSON for making a legacy response readable, and JSON to XML for talking to a system that still requires it. SOAP endpoints, RSS feeds, sitemaps, bank and government APIs and older enterprise software all still speak XML.
How XML becomes JSON
The two formats do not map cleanly onto each other, so the mapping has to make choices. This one uses conventions you can predict:
- An element's attributes become keys prefixed with
@_, so<user id="7">yields"@_id": "7". - An element's text content, when it also has attributes or children,
becomes a key called
#text. - Repeated sibling elements become an array.
That last point is where automatic conversion is genuinely lossy, and it is
worth understanding: XML cannot distinguish "a list that happens to have one
item" from "a single element". Two <item> siblings become an array of two; one
<item> becomes a single object, not an array of one. Code that assumes an
array will break on the single-item case. If you are consuming converted XML,
normalise that field before using it.
You can also strip attributes entirely, which produces much cleaner JSON when the attributes are metadata you do not need.
How JSON becomes XML
XML requires exactly one root element, so a JSON array or a document with several top-level keys is wrapped in one, and you are told that this happened.
XML element names are also stricter than JSON keys: they cannot begin with a
digit, cannot contain spaces, and cannot start with the letters xml. Keys that
would produce invalid XML are reported with their paths, so you can rename them
rather than shipping a document that no parser will accept.
Formatting existing XML
If you only need to make a single-line XML document readable, use the XML formatter instead — it indents without changing the document into something else.
Processed in the page
Both directions run locally. XML payloads from finance and government systems tend to contain exactly the data you should not paste into an unknown website.
questions
- How are XML attributes represented in JSON?
- As keys prefixed with @_, so an element written as user id="7" produces "@_id": "7". Text content alongside attributes or children becomes a key named #text. You can also strip attributes entirely for cleaner output.
- Why is my single XML element not an array?
- Because XML cannot distinguish a one-item list from a single element. Two sibling item elements become an array of two; one becomes a single object. Code that assumes an array must normalise that field before using it.
- What happens when JSON has no single root?
- XML requires exactly one root element, so an array or a document with several top-level keys is wrapped in one and you are told that it happened.
- Why were some of my JSON keys rejected?
- XML element names are stricter than JSON keys: they cannot begin with a digit, contain spaces, or start with the letters xml. Offending keys are reported with their paths so you can rename them rather than producing a document no parser will accept.
- I only want to indent my XML, not convert it.
- Use the XML formatter instead. It reformats the document without changing it into another format.