XML to JSON Converter

Paste XML and convert it to JSON. Repeated elements become arrays, attributes are kept under an @attributes key, and everything runs in your browser.

Indent:
Copied
XML Input0 B
JSON Output0 B

What this XML to JSON converter does

The converter parses your XML with the browser’s built-in DOMParser, then walks the element tree and builds a JSON object from it. The root element becomes the single top-level key, child elements become nested keys, repeated elements become arrays, attributes are grouped under @attributes, and every value is a string. The output is indented with 2 or 4 spaces using JSON.stringify.

XML and JSON do not have the same building blocks. XML has elements, attributes, text, mixed content, comments, CDATA sections and namespaces; JSON (RFC 8259) has objects, arrays, strings, numbers, booleans and null. Some information therefore has to be reshaped or dropped, and knowing which saves surprises later.

How each part of an XML document is mapped

XMLJSON produced
Element with text only, no attributesA string: <n>007</n> → "n": "007"
Element with child elementsAn object with one key per child name
Same element name twice or more under one parentAn array of the values, in document order
AttributesAn "@attributes" object on that element
Text next to attributes or child elementsA "#text" key
Empty element <b/>An empty string ""
CDATA sectionText, with its markup kept literally
Entity and character referencesDecoded: A &amp; B &#169; → "A & B ©"
Namespace prefixes, xmlns declarationsKept in key names ("soap:Body") and as attributes
Comments and processing instructionsDropped

Leading and trailing whitespace is trimmed from each piece of text, including inside CDATA, so indentation in the source does not end up in the JSON. Line breaks in the middle of a text value are kept.

How to use the converter

  1. Paste XML into the XML Input pane, open a file with File, or load one with URL (the remote server must allow cross-origin requests).
  2. Choose an Indent of 2 spaces or 4 spaces for the JSON.
  3. Click Convert. If the XML is not well-formed, an error starting with Invalid XML: shows the parser’s explanation. That text comes from the browser’s XML parser, so Chrome, Firefox and Safari word it differently.
  4. Copy the result, Download it as converted.json, or Clear both panes.

Until you convert, the output pane shows the XML input as a tree, with attributes listed as @name and repeated elements numbered, for example book[1]. Click Tree Viewer to return to it after converting; clicking a node copies its path.

Example

<catalog>
  <book id="bk101" lang="en"><title>XML Developer's Guide</title><price>44.95</price></book>
  <book id="bk102"><title>Midnight Rain</title><price>5.95</price></book>
</catalog>

converts to (shown compacted here):

{
  "catalog": {
    "book": [
      { "@attributes": { "id": "bk101", "lang": "en" }, "title": "XML Developer's Guide", "price": "44.95" },
      { "@attributes": { "id": "bk102" }, "title": "Midnight Rain", "price": "5.95" }
    ]
  }
}

Watch out for single versus repeated elements

Whether a key holds an object or an array depends on the document, not on a schema. With two <item> elements, order.item is an array; with only one, the same key holds a single object. Code that reads the JSON should accept both, for example by normalising with [].concat(order.item) in JavaScript.

Mixed content is the other weak spot. In <p>Hello <b>world</b> and <i>you</i>!</p> the text fragments are joined with spaces into "#text": "Hello and !", while b and i become separate keys, so the original word order is lost. For document-style XML such as XHTML or DocBook, keep the content as XML or transform it with XSLT instead.

Common problems and fixes

ProblemCauseFix
Error about a mismatched or unexpected closing tagA tag is not closed, or HTML habits such as a bare <br> are used.Close every element (<br/>) and check nesting in the XML Formatter.
Error mentioning an entity such as nbspXML only predefines &amp; &lt; &gt; &quot; &apos;; HTML entities are undefined.Use a numeric reference such as &#160;.
Error pointing at the start of a second top-level elementTwo top-level elements, e.g. <a/><b/>.Wrap them in one parent element.
Error near a bare &An unescaped ampersand in text or a URL.Write it as &amp;.
Numbers and booleans are stringsXML text has no types and the converter does not guess.Convert the fields you need in code; this keeps values such as 007 intact.

Frequently asked questions

Why are my numbers and booleans strings in the JSON?

XML stores everything as text, and guessing types would corrupt values such as postcodes, phone numbers and IDs with leading zeros. <active>true</active> therefore becomes "true". Cast the specific fields you need in your own code.

Can I convert the JSON back to the same XML?

Not directly. The JSON to XML converter expects attributes as keys starting with a hyphen, so an @attributes object would be turned into an element called _attributes. Rename those keys to -id style first if you need a round trip.

What happens to XML comments?

They are removed, together with processing instructions such as <?xml-stylesheet ?>. JSON has no comment syntax, so there is nowhere to put them.

Does it work with SOAP responses, RSS feeds and sitemaps?

Yes, as long as the document is well-formed. Namespaced names are kept with their prefix, so a SOAP body appears under "soap:Envelope" → "soap:Body", and the xmlns:soap declaration is listed under @attributes.

Is there a size limit?

There is no fixed limit in the tool; the practical limit is your browser’s memory. Very large documents can make the page slow while they are parsed and while the tree view is drawn.

Is my XML sent to a server?

No. Parsing and conversion happen in your browser, so internal API responses and configuration files never leave your device.

Related tools