Convert XML to JSON

Runs on your device · Your file never leaves your device.

Drop an .xml file or paste XML from an API, a feed or a configuration file and get JSON you can work with. The document is read by the browser’s own XML parser, in the encoding its first line declares, so anything that is well-formed converts, and a malformed file is reported with the line and column of the problem and what is wrong there.

The mapping is fixed and reversible: each element becomes a key; siblings with the same name become an array; attributes are kept under keys starting with @ (id="A3" becomes "@id": "A3"); and when an element holds both text and children or attributes, the text goes under "#text". An element with only text becomes the value itself.

With type detection on, text such as 12, true or an empty element becomes a number, a boolean or null; switch it off to keep everything as text. Namespaced names (soap:Body) are kept as written. The preview shows the first 200 lines before you download.

How to convert XML to JSON

  1. Drop an XML file, or paste XML into the box.
  2. Decide whether numbers, booleans and empty elements should be typed.
  3. Press Convert to JSON and check the preview.
  4. Download the .json.

Supported formats and limits

Input
XML (.xml) — a file or pasted text, in the encoding it declares
Output
JSON, pretty-printed, UTF-8
Attributes
Kept as "@name" keys
Repeated elements
Become an array
Where it runs
In your browser; nothing uploaded

Not for: Documents that are not well-formed XML (unclosed tags, HTML pages) — the parser reports the first problem rather than guessing.

Which values change type

Type detection applies to attributes as well as to text, and changes a value only where nothing is lost. With it on, id="3" becomes "@id": 3, while id="007" stays the text "007". 1.50 also stays text, because as a number it would be written back as 1.5. TRUE becomes true. An empty element such as <note/>, and one holding only spaces, become null; the word NULL stays text. Dates such as 2024-01-05 are never converted.

With detection off, empty elements become "" and every value stays text exactly as written.

What the JSON cannot keep

Some parts of an XML document have no place in JSON and are dropped: comments, processing instructions and the DOCTYPE. CDATA sections become ordinary text, so <a><![CDATA[<b>x</b>]]></a> gives "a": "<b>x</b>".

Order between elements with different names is lost, because each name becomes one key. In <step>1</step><note/><step>2</step>, both steps are gathered into "step": [1, 2], and nothing records that the note sat between them. Text mixed with markup loses its place in the same way: <p>Hello <b>world</b> and <i>you</i>!</p> becomes "b": "world", "i": "you" and "#text": "Hello and !". For documents where that order is the content, such as formatted prose, this mapping is not a good fit.

Namespaces, entities, errors and encodings

Prefixed names are kept as written, and namespace declarations appear as attributes: <soap:Envelope xmlns:soap="…"> gives a key "soap:Envelope" holding "@xmlns:soap", and /json-to-xml turns both back into the same XML.

Apart from entities that the document's own DOCTYPE defines, only the five built into XML (&amp; &lt; &gt; &quot; &apos;) and numeric ones such as &#160; are understood. When the parser stops, the message gives the line and column and says what is wrong in plain words: an element closed with another name, an unknown name such as &nbsp; (write &#160; instead), a bare & or <, a second top-level element, or a file that ends too early.

The file is read in the encoding its first line declares, such as ISO-8859-1 or windows-1256. Without a declaration, UTF-8 or UTF-16 is read, and older code pages are recognised as described on /csv-to-json.

Frequently asked questions

Are my files uploaded to a server?
No. This conversion runs inside your browser using your own device’s processing power, and the file is not uploaded. Once a tool has been used once (video, text recognition and background removal download their engine from this site the first time), it keeps working when your connection drops.
How are attributes and mixed content handled?
Attributes become keys prefixed with @, so <item id="A3"> gives "@id": "A3". If an element has text as well as attributes or child elements, the text is stored under "#text"; an element with text only becomes that text directly.
Why is one element an object and the repeated ones an array?
An element that appears once under its parent becomes an object (or a value); two or more siblings with the same name become an array. That mirrors the document exactly, so a list of one item is one object — if your code needs an array in both cases, wrap it on the receiving side.
What does the top of the JSON look like?
One key named after the root element, holding everything inside it — <records>…</records> becomes {"records": {...}}. That keeps the root name so the JSON to XML tool can rebuild the same document.

Related tools

More in Spreadsheets & data

This page in other languages: العربية · Deutsch · Français · Español · Русский · Português · 中文 · हिन्दी · Türkçe · עברית · فارسی · اردو · Bahasa Indonesia · বাংলা