Skip to content

XML to JSON

Convert XML to a JSON object.

Input
Output

No result

About XML to JSON

XML to JSON reads a document and produces an object that mirrors its element structure. Attributes are turned into keys prefixed with @ so they remain distinguishable from child elements, text content of a leaf element becomes the value directly, and when sibling elements share a name they are collected into an array.

Those three conventions are what make the conversion reversible in practice. Mixing attributes and child elements into the same namespace would lose the distinction the original document made, and collapsing repeated elements into a single value would silently discard data whenever a document legitimately contains several.

Documents that are not well-formed are rejected with an error rather than being partially converted. That is the right outcome for integration work: a malformed response is a problem to surface immediately, not something to coerce into a structure that appears to have worked.

The conventions above are fixed and cannot be adjusted. The attribute prefix is always @, the text key is always #text, and repeated siblings always become an array from the second occurrence onwards — so a consumer expecting a uniform attribute prefix, or expecting a single-element array to stay an array, cannot be accommodated here. Namespaces are reduced to their literal prefixed names with no separate resolution step, and processing instructions, comments and document type declarations are not represented in the output at all. Mixed content — an element holding both text and child elements — is also only approximated, since JSON has no structure that matches it cleanly.

How to use XML to JSON

  1. 1 Paste the XML document.
  2. 2 The equivalent JSON appears immediately, with attributes under @-prefixed keys.
  3. 3 Check repeated elements — they are collected into arrays automatically.

Frequently asked questions

Why do some keys start with an @ sign?

That prefix marks an XML attribute. Attributes and child elements are different things in XML, and JSON has no way to distinguish them on its own, so the prefix preserves the distinction in the converted output.

When do sibling elements become an array?

When more than one element shares the same name under the same parent. A single occurrence stays a plain value, which keeps simple documents readable; a second occurrence causes both to be wrapped in an array so neither is lost.

Where does text content go?

For an element with no children, the text becomes the value directly. If the element also has attributes, the text is placed under the key #text so it can sit alongside them without collision.

What if the XML is malformed?

The conversion reports an error instead of producing partial output. That is deliberate — a partially parsed document looks like a successful conversion and can propagate a silent data problem much further than an outright failure would.

Free, online, and private

Free to use

XML to JSON is completely free. There is no account to create, no login, and no hidden charge or usage limit.

Runs online in your browser

It is a free online tool that runs entirely in your browser. Nothing to download or install — open the page and start using it right away.

Your data stays private

All processing happens locally on your device. Your input is never uploaded, stored, or tracked, so it never leaves your browser.

How XML to JSON behaves in detail

These are the specifics of this implementation — the exact ranges, defaults and fixed settings you are working with.

Parameter Value
Parsing DOMParser with text/xml, followed by an explicit parsererror check that throws Invalid XML
Attributes copied onto the object with an @ prefix, so an id attribute becomes @id
Text-only leaves returned as a plain string, trimmed of surrounding whitespace
Leaves with attributes the text is placed under the key #text so it cannot collide with an attribute name
Repeated tags the second occurrence turns the value into an array of [first, second]
Output indent 2 spaces

Compiled from the implementation of this tool and checked against it by itoolnet.

What XML to JSON does not do

Knowing the edges of a tool saves more time than another list of its features. These are the cases where it will not do what you might expect.

  • Text is trimmed and mixed content is dropped. For a paragraph holding text and a nested element, the code walks only the element children, so the leading text disappears and only the nested subtree survives in the JSON.
  • The shape of a field is unstable across documents. One occurrence of a tag gives a string while two give an array, so a consumer cannot rely on a fixed type and has to inspect every field before using it.
  • Everything is a string, an object or an array — there is no type inference. A numeric field holding 30 comes back as the string "30" and a boolean-looking true stays text, so casting is left to the reader.
  • There is no option to control the mapping. The @ prefix for attributes, the #text key for mixed leaves, the 2-space indent and the rule that turns repeats into arrays are all hard-coded.

Tested and documented by itoolnet, the team that builds and maintains this site.

Share & embed this tool

Writing a guide or article? You are welcome to link to XML to JSON with the snippet below. It is free, runs entirely in your browser, and needs no signup.