Skip to content

XML to JSON Converter

Parse XML documents and convert to structured JSON, with attributes kept as @_-prefixed keys and well-formedness checks.

XML to JSON Converter tool

Loading...

Example

Input: <user id="7"><name>Ada</name><age>36</age></user>

Result: A 2-space indented JSON object: { "user": { "name": "Ada", "age": 36, "@_id": "7" } }

Child elements become keys and numeric text such as 36 becomes a number. The id attribute is kept with an @_ prefix, here as the string "7".

XML to JSON Conversion Rules

XMLJSON
<a>36</a>{"a": 36}
<a>1.50</a>{"a": 1.5}
<a>007</a>{"a": "007"}
<a>true</a>{"a": true}
<a/>{"a": ""}
<a> hi </a>{"a": "hi"}
<a id="7">x</a>{"a": {"#text": "x", "@_id": "7"}}
<a><b>1</b><b>2</b></a>{"a": {"b": [1, 2]}}
<a><![CDATA[<b>]]></a>{"a": "<b>"}
<ns:a>x</ns:a>{"ns:a": "x"}

Output of this converter.

  1. Paste your XML document into the XML panel.
  2. Attributes are converted automatically to @_-prefixed keys and element text alongside them to #text keys.
  3. The parser validates your XML and reports any well-formedness errors with line and column numbers.
  4. Review the structured JSON output in the right panel with proper indentation.
  5. Click the copy button to copy the JSON for use in your modern application or API integration.

How XML Converts to JSON

This tool parses an XML document and writes it as formatted JSON. Child elements become keys, repeated elements become arrays, and attributes become keys that start with @_. So <user id="7"><name>Ada</name></user> becomes {"user": {"name": "Ada", "@_id": "7"}}.

The XML is checked first. If a tag is not closed or elements overlap, you get an error with the line and column instead of a half-converted result. Text that looks like a number or a boolean becomes one: <age>36</age> gives 36 and <ok>true</ok> gives true. Values with leading zeros (007) and hex text (0x1F) stay strings, so ZIP codes and IDs survive. Text is trimmed of surrounding spaces, and an empty element becomes an empty string. Attribute values are always kept as strings.

One pattern to watch: an element that appears once becomes a single value, while one that repeats becomes an array. So one item inside items gives "item": 1, but two give "item": [1, 2]. Code that reads the JSON should handle both shapes.

When an element has attributes or children and also text, the text goes into a #text key. CDATA content becomes plain text, comments are dropped, and namespace prefixes stay in the key, as in "ns:item".

The panels also work backwards, rebuilding XML from JSON that uses the same @_ and #text conventions. For control over the root name and indentation in that direction, use the JSON to XML Converter. To tidy the source XML first, try the XML Formatter.

Sources

Frequently Asked Questions

Each attribute becomes a key with an @_ prefix inside the element's object, and its value is always a string. For example, <item id="1">x</item> becomes {"item": {"#text": "x", "@_id": "1"}}. The fixed prefix keeps attributes apart from child elements of the same name and lets the reverse conversion rebuild them.
The parser only sees what is in your document. If an element appears once under its parent, it becomes a plain value. If it appears two or more times, it becomes an array. When your code expects a list, wrap single values in an array yourself, for example with Array.isArray(x) ? x : [x] in JavaScript.
Element text that looks like a number is converted to a JSON number, and numbers do not keep trailing zeros, so 1.50 becomes 1.5. Values with leading zeros and hex values are deliberately kept as strings, because they are usually codes rather than amounts. If you need 1.50 exactly, read it as a string in your code.
The child elements become keys and all the loose text is joined into a single #text value. For <p>Hello <b>you</b> there</p> you get the b element plus the text Hellothere, with the word order and spacing lost. For documents written in prose, such as XHTML, a DOM parser is a better fit than a data conversion.
The XML declaration at the top of the file, such as <?xml version="1.0" encoding="UTF-8"?>, is kept as a key named ?xml, with its version and encoding stored as @_ attributes. It is metadata about the file rather than data, so you can safely delete it from the JSON if your code does not need it.

Related Tools

JSON to XML Converter

Convert JSON data to well-formed XML documents with customizable root element and formatting options.

Developer Tools

XML Formatter and Prettifier

Format, indent, and validate XML documents with customizable indentation and output options.

Developer Tools

JSON Formatter and Validator

Format, validate, beautify, and minify JSON data with a collapsible tree view and line-and-column error detection.

Developer Tools

JSON to YAML Converter

Convert JSON documents to YAML format with syntax validation, clean formatting, and instant preview.

Developer Tools

CSV to JSON Converter

Convert CSV data to a JSON array of objects with header mapping and automatic comma, semicolon, or tab detection.

Developer Tools

JSON to CSV Converter

Convert a JSON array of objects to CSV with automatic column headers, and convert CSV back to JSON in the same tool.

Developer Tools