Skip to content

XML to JSON Converter

Convert XML to JSON as you type, and choose how attributes, repeated elements and values are mapped.

Input · XML
⇄
Output · JSON
Valid XMLRuns in your browser

Load from a URL

Your browser fetches it directly, so the server must allow cross-origin requests (CORS).

How to convert XML to JSON

  1. Paste XML, open a file or use From URL. The JSON updates as you type.
  2. Adjust the options below.
  3. Press ⇄ to open the result in JSON to XML.

Broken XML is not converted: the output shows the error with its line and column, explained in the XML validator.

Example

<order id="A-1001" paid="true">
  <customer zip="02134">Lena Vogel</customer>
  <item sku="BK-7" qty="2">Notebook</item>
  <item sku="PN-3" qty="1">Fountain pen</item>
  <note/>
</order>

With the default options:

{
  "order": {
    "customer": {
      "#text": "Lena Vogel",
      "@zip": 2134
    },
    "item": [
      {
        "#text": "Notebook",
        "@sku": "BK-7",
        "@qty": 2
      },
      {
        "#text": "Fountain pen",
        "@sku": "PN-3",
        "@qty": 1
      }
    ],
    "note": "",
    "@id": "A-1001",
    "@paid": true
  }
}

Text next to attributes goes under #text, and the empty <note/> becomes "".

Options

  • Attributes: the prefix for attribute keys, @name, _name, -name or plain name. Without a prefix, an attribute and a child element of the same name collide.
  • Numbers & booleans: "2" becomes 2 and "true" becomes true, in text and attributes. It also turned zip="02134" into 2134 and reads 0x1F as 31, so turn it off for postcodes, phone numbers and IDs.
  • Always arrays: every element becomes an array, the root included. Without it, two <item> elements give an array but one gives an object, which breaks code that loops over order.item.

What JSON cannot keep

  • Comments, processing instructions and the <?xml ?> declaration are dropped; CDATA becomes plain text.
  • Namespace prefixes stay in the key (soap:Body), and xmlns declarations appear as attributes.
  • Mixed content such as <p>Hello <b>world</b>, again</p> loses its shape: the text pieces join into one #text and their position around <b> is lost.

For a few values, an XPath tester query is quicker than converting. Large results are easier to read in the JSON viewer.

Questions

Is there a standard way to map XML to JSON?

No. Libraries differ on attributes, text and repeated elements, hence the options. The default @ prefix and #text key are the names Json.NET uses.

Can I get the original XML back?

Mostly. Attributes and text survive the trip through JSON to XML; comments, the order between differently named siblings, and values rewritten by Numbers & booleans (02134 comes back as 2134) do not.

How do I do the same conversion in Node.js?

This page uses fast-xml-parser: new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "@" }).parse(xml) gives a similar result.

Related tools

Esc