XML to JSON

Convert XML to JSON format

About XML to JSON

XML to JSON checks pasted XML and converts it with the fast-xml-parser library. Each element becomes a key; repeated sibling elements become an array; attributes become keys prefixed with @_; text that shares an element with attributes or children goes under #text. Every value stays a string exactly as written, so leading zeros and trailing decimals survive. The XML declaration is dropped. Malformed XML, a second root element included, is reported with a message that gives the line and column of the error, instead of a partial result. fast-xml-parser does the parsing inside your browser tab.

Example

Two person elements with an id attribute and phone numbers starting with 0, and the JSON the tool returns:

XML

<?xml version="1.0"?>
<contacts>
  <person id="1"><name>Ann</name><phone>0612345678</phone></person>
  <person id="2"><name>Bo</name><phone>0698765432</phone></person>
</contacts>

JSON

{
  "contacts": {
    "person": [
      {
        "name": "Ann",
        "phone": "0612345678",
        "@_id": "1"
      },
      {
        "name": "Bo",
        "phone": "0698765432",
        "@_id": "2"
      }
    ]
  }
}

How to convert XML to JSON

  1. 1Paste XML into "XML input".
  2. 2Click "Convert"; the button reads "Converting…" while the parser loads.
  3. 3Read the JSON in "JSON output", with a note if some elements mix text and tags.
  4. 4Click "Copy", or "Download" to keep the JSON as data.json.

Formats and limits

Input
XML text
Output
indented JSON in data.json
Attributes
keys prefixed with @_
Values
always strings, never turned into numbers

Where your file is processed

fast-xml-parser is downloaded into the page when you first click "Convert" and does all the work in your browser; your XML is not uploaded. A validation error or any other failure is reported to us as text, quoted names and values masked, with the tool name and your browser name and version.

Frequently asked questions

Are XML attributes kept?

Yes. Each attribute becomes a key prefixed with @_ on the element's object, next to its children, so a person element with id 1 gets @_id holding the string 1.

Are numbers converted to JSON numbers?

No. Every value stays text exactly as written, so the phone number 0612345678 keeps its zero and a version 1.10 is not shortened. Convert the values you need in your own code.

Is text mixed with tags kept in place?

No. In <p>Hello <b>world</b> again</p>, the text parts are joined under #text without their position among the tags, and the page shows a note naming such elements. The tool suits data files better than documents.

Will invalid XML be detected?

Yes. The XML is validated first, and the message ends with the line and column of the error, as in char '&' is not expected. (line 2, column 6). A second root element, as in <a></a><c/>, is refused too, because XML allows only one.

Tips and tricks

  • ✓To write JSON as XML, with @_ keys turned into attributes, use JSON to XML.