How to convert XML to JSON
- 1Paste your XML into the input panel. The converter checks it as you type and shows the line and column of any error.
- 2The JSON appears on the right straight away. Attributes get an @ prefix, text mixed with attributes goes under #text, and elements that repeat become arrays.
- 3Adjust the options if you need to: change the attribute prefix or text key, always use arrays, or convert numbers and booleans from strings to real JSON types.
- 4To convert JSON to XML, choose the JSON to XML tab and paste your JSON. Set the root element name and decide whether to add an XML declaration.
- 5Copy the result or click Save to download converted.json or converted.xml.
How XML maps to JSON
XML and JSON describe data differently, so there is no single correct way to convert XML to JSON. XML has attributes, ordered child elements, text mixed with elements, comments and namespaces; JSON has objects, arrays and plain values. This converter uses the convention most APIs and libraries share, so the output looks familiar and converts back cleanly.
An element with only text becomes a string, so <title>Dune</title> is "title": "Dune". An element with attributes becomes an object whose attribute keys start with @, so <book id="1"> gives "@id": "1". If that element also holds text, the text is stored under #text. When the same child element appears more than once, the children are collected into an array. An empty element such as <br/> becomes null. Text that sits between child elements, as in <p>Hello <b>world</b>!</p>, is kept in order under #mixed as ["Hello ", {"b": "world"}, "!"], with no spaces added.
Going the other way, the JSON to XML converter follows the same rules. Keys that start with @ become attributes, #text becomes the element's text, arrays become repeated elements and null becomes an empty element. If the JSON has more than one top-level key, or is an array, it is wrapped in a root element whose name you choose. Keys that are not valid XML names, such as ones with spaces, are reported by name and path rather than silently changed.
Because XML stores everything as text, "42" and "true" stay strings unless you ask the converter to turn numbers and booleans into real JSON types. Comments and the XML declaration are dropped, since JSON has no place for them. The converter runs in your browser and never uploads what you paste.
When developers reach for it
Consuming legacy XML APIs
Convert a SOAP or XML API response to JSON to inspect it, build a mock, or write a test fixture for a JavaScript client.
Migrating config and data files
Move data from an XML export, such as an RSS feed or sitemap, into a JSON structure that a new service can load.
Building XML requests
Convert JSON to XML to produce a request body for a service that only accepts XML, then adjust attributes with the @ prefix.
Checking a mapping
Convert an XML sample to JSON and back to see exactly how attributes, arrays and empty elements are represented.
Frequently asked questions
How are XML attributes converted to JSON?
+
Each attribute becomes a key in the element's object, with a prefix so it can be told apart from child elements. The default prefix is @, so id="7" becomes "@id": "7". You can change the prefix or set it empty.
How does the converter handle repeated elements?
+
Elements with the same name under one parent are collected into an array. A single element stays a plain value unless you turn on Always use arrays, which keeps the shape the same whether there is one item or many.
Can I convert JSON to XML too?
+
Yes. Open the JSON to XML tab to convert JSON back to XML. Keys that start with the attribute prefix become attributes, arrays become repeated elements, and you can pick the root element name.
Why are my numbers strings in the JSON?
+
XML has no number type, so every value is text. Tick Convert numbers and booleans to turn values such as 42, 3.5 and true into JSON numbers and booleans. Values with leading zeros or too many digits to be exact are left as strings.
What happens to comments and the XML declaration?
+
They are left out of the JSON, because JSON has no equivalent. CDATA sections are read as ordinary text, and entities such as & are decoded.
Is my XML sent to a server?
+
No. The XML is parsed and converted in your browser, so confidential feeds and API responses stay on your device.
Related tools
XML Formatter & Validator
Format, validate and minify XML online. Choose the indent, see the line and column of any error, and download the result. Runs in your browser.
JSON Formatter
Format and beautify JSON data with syntax highlighting and validation.
YAML to JSON Converter
Convert YAML configuration files to JSON format with syntax validation and error checking.
JSON to CSV Converter
Convert JSON arrays to CSV and CSV back to JSON. Handles quoted commas and multi-line fields, flattens nested objects to dot-path columns, and supports comma, semicolon, tab and pipe delimiters.
JSON Compare
Compare two JSON documents and see added, removed and changed paths side by side, with an option to ignore key order. Private and in-browser.