Nimble Toolbox

Convert XML to JSON

Paste XML or drop an .xml file, such as a feed, a SOAP response, or an export, and the JSON appears as you type. Your browser parses it, so the document stays on your machine.

?

How to use it#

  1. Drop an .xml file on the box, or paste the XML into it.
  2. Choose indented or minified JSON, and whether numbers and booleans become real JSON values.
  3. Copy the JSON, or download it as a .json file named after the one you dropped.

How the XML maps onto JSON#

XML has two things JSON does not: attributes, and text that sits beside other content. Each needs a convention, and this page uses one of the most common. An attribute becomes a key with @ in front of its name, so id="7" becomes "@id": 7. Text inside an element that also has attributes or children goes under a key called #text. An element holding nothing but text is simply that text, which keeps the everyday case short.

The root element becomes the single top-level key, so a document that starts with <catalog> becomes an object with one key, catalog. Namespace prefixes are kept as part of the name, soap:Body stays soap:Body, and the xmlns declarations come through as ordinary @ attributes. So a SOAP response, an RSS or Atom feed, a sitemap.xml, or a Maven pom.xml converts like any other document.

The JSON to XML converter reads the same convention, so a document converted here and back comes out as the same XML, formatted. The exceptions are the things JSON has no room for, covered below.

Why an element is sometimes an array and sometimes not#

XML repeats an element to make a list, and JSON needs an array for that. When an element appears more than once under the same parent, its values become an array; when it appears once, it is a single value. That is the right reading of each document on its own, and it is also the most common surprise: an order with one line item gives an object where an order with two gives an array.

Code that reads the JSON should expect either. In JavaScript, [].concat(value) turns both forms into an array in one step. There is no way to tell from a single document which elements are meant to repeat, so no converter can get this right without a schema.

What is left behind#

Comments, processing instructions, and the DOCTYPE are dropped, since JSON has nowhere to put them. CDATA sections are read as the text they contain. Leading and trailing whitespace inside an element is trimmed, which is what almost every XML file indented for reading needs, but which does change a value whose spaces were deliberate.

Mixed content is the real limit. In <p>Hello <b>world</b>.</p> the text and the bold element are interleaved, and JSON objects have no order between different keys, so the result keeps the words and the element but not where the element sat in the sentence. XML used for data rarely does this; XHTML and document formats do all the time, and for those the XML formatter is the better tool.

Frequently asked questions#

What do the @ and #text keys mean?

#

A key starting with @ was an attribute on the element, and #text holds the element's own text when it also has attributes or children. So <price currency="EUR">9.99</price> becomes {"price": {"@currency": "EUR", "#text": 9.99}}. Neither character can start an XML element name, so these keys never clash with a real element.

Why is my number still a string?

#

Text becomes a number only when writing that number back gives exactly the same text. So 42 converts, while 007, 1.50, and 1e3 stay strings, because as numbers they would come back as 7, 1.5, and 1000. That rule is what lets the JSON convert back to the XML it came from. Turn the conversion off to keep every value a string.

Does it convert back to XML?

#

Yes. The JSON to XML converter uses the same @ and #text convention, so the round trip gives the same XML, formatted, apart from comments, whitespace at the ends of text, and the order inside mixed content.

Is there a size limit?

#

Only your browser's memory. A few megabytes of XML convert almost at once. Very large exports are better handled by a streaming parser in a script, since the whole document and its JSON both have to fit in the page.

Is my XML uploaded?

#

No. It is parsed by your browser in this tab, and nothing is sent anywhere.