DevSwitch.net
Back to Tools
⇄

XML to JSON Converter

Convert XML data to JSON format

File Converters

Tip: XML structure will be converted to JSON objects and arrays.

Runs in your browser

Input XML

Convert XML to JSON in your browser

Paste XML, open an .xml file or drop it on the input window, and the JSON appears as you type. The JSON is the content of the root element: each child element becomes a key, with its attributes and text kept beside it. Copy the result or download it as a .json file.

The XML is read by your browser's own XML parser. A tag that is never closed or a broken nesting shows the parser's error in the output window instead of half a result.

Not sure which format your project needs? XML vs JSON in modern APIs compares the two.

XML to JSON example

Real output for a catalog of two books. The id attributes become @id keys, the two <book> elements become an array, and the price stays a string:

catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book id="bk101">
    <title>Paper Tigers</title>
    <author>Rina Okafor</author>
    <price>12.50</price>
  </book>
  <book id="bk102">
    <title>Salt Roads</title>
    <author>Jonas Hale</author>
    <price>9.99</price>
  </book>
</catalog>
catalog.json
{
  "book": [
    {
      "@id": "bk101",
      "title": "Paper Tigers",
      "author": "Rina Okafor",
      "price": "12.50"
    },
    {
      "@id": "bk102",
      "title": "Salt Roads",
      "author": "Jonas Hale",
      "price": "9.99"
    }
  ]
}

How XML elements map to JSON

JSON has no attributes and no mixed content, so each XML part gets a fixed place:

  • XML

    <book id="bk101">

    JSON

    An attribute becomes a key with @ in front, @id, in the same object as the child elements.

  • XML

    <price>12.50</price>

    JSON

    An element with only text becomes a plain value. It is always a string: XML has no types, so 12.50, true and 007 come through as written.

  • XML

    <book/><book/>

    JSON

    Two or more elements with the same name become an array under that name. A single <book> stays an object, not an array of one.

  • XML

    <note lang="en">Ring twice</note>

    JSON

    Text next to attributes or child elements goes in a #text key, so this note becomes an object with @lang and #text.

  • XML

    <phone/>

    JSON

    An empty element becomes an empty string.

What carries over from the XML

Kept

  • Namespace prefixes, as part of the name: dc:title stays the key dc:title
  • Namespace declarations, as attributes such as @xmlns:dc
  • Text inside CDATA sections, as plain text

Dropped

  • The root element's name: the JSON starts with what is inside it
  • Comments, processing instructions and the XML declaration
  • Spaces and line breaks at the start and end of each text

Need the XML back?

The JSON to XML converter writes @ keys back as attributes and arrays as repeated elements.

XML to JSON questions

Click Open file and choose the .xml file, or drop it on the input window. The JSON shows up at once; Download saves it as a .json file.

Arrays come only from repeated elements. A list with one item in the XML gives an object, so if a list can hold one or many items, have your code accept both shapes.

XML stores every value as text, and the converter does not guess types. An ID like 007 keeps its zeros and a price like 12.50 keeps its trailing zero; turn fields into numbers in your code where you need them.

Prefixes are kept as part of each name, so <dc:title> becomes the key dc:title, and the xmlns:dc declaration becomes an @xmlns:dc key.

No. Converting runs in your browser, and an opened file is read there too.