Turn XML into JSON, and back again
This tool converts an XML document to JSON and JSON to XML. Handy when a product feed or an RSS file arrives as XML but your code expects JSON. Everything runs in your browser with its built-in XML parser: nothing is uploaded, and files up to 10 MB are fine.
How to use it
- Pick a direction: "XML → JSON" or "JSON → XML".
- Paste your text, click "Open a file" or drop the file onto the input box.
- Set the conventions: "Attribute prefix", "Convert numbers and booleans", "Always use arrays", "CDATA sections" and "Keep comments (#comment)"; in the other direction, "Root element name (if needed)" and "XML declaration at the top".
- Grab the output with "Copy" or "Download". "Swap (round trip)" feeds the output back in, so you can check it rebuilds your original.
The conventions, with exact examples
- Attributes get a
@prefix (or-if you prefer).id="1"becomes"@id": "1". - Repeated elements: two
<author>elements become an array. - Text next to attributes goes into
"#text".<price currency="USD">12.50</price>gives{"@currency": "USD", "#text": "12.50"}. - CDATA is kept apart in
"#cdata", or merged into the text. - Comments are dropped, or kept in
"#comment". - Namespaces are kept as written.
<dc:title>becomes the key"dc:title"and thexmlns:dcdeclaration becomes"@xmlns:dc".
Feed it <book id="1"><title>A</title><author>X</author><author>Y</author></book> and you get {"book": {"@id": "1", "title": "A", "author": ["X", "Y"]}}. Tick "Convert numbers and booleans" and "@id" becomes the number 1. Tick "Always use arrays" and the title turns into ["A"], handy when your code always expects a list.
Number conversion only touches values written in one unambiguous way: 4.5, -2, true, false. A ZIP code such as 02134, a price written 12.50 or 1e3 stay as text, so no leading zero or trailing digit is lost.
From JSON to XML
The same rules run in reverse: a @ key becomes an attribute, #text the text, an array a run of repeated elements and null an empty element. Reserved characters are escaped: {"note": "a < b & c"} gives <note>a < b & c</note>, and a double quote inside an attribute becomes ".
A key that cannot be used as a tag name is fixed and reported: "2nd reminder" becomes <_2nd_reminder/> (a _ prefix, the space replaced). If the JSON has several top-level keys, or is an array, everything goes inside a root element, <root> by default.
Errors and safety
Malformed XML (a <b> tag never closed, a bare &) or invalid JSON is reported with its line and column when the browser provides them. The XML is parsed in a separate document and never rendered as a page, and the browser's parser never loads external entities: a DOCTYPE pointing at a remote file triggers no request at all.
FAQ
Why is there no single correct XML to JSON conversion?
Because XML separates attributes, text and element order, and JSON has no way to express those differences. Whether an element appears once or twice even changes the shape of the JSON (a value or an array). Every converter picks its own conventions; this one shows them and lets you change them.
Does a round trip give me back exactly the same XML?
The data, yes: attributes, text, repeated elements, CDATA and, if you keep them, comments. Formatting, processing instructions and the position of text mixed with elements are not kept; the tool warns you in that last case.
Why does "12.50" stay in quotes?
As a number it would become 12.5, and the rebuilt XML would no longer match. Only values that come back unchanged are converted.
Are my files sent anywhere?
No. Everything happens in the open tab. Only your settings are remembered for your next visit.