Skip to main content
AI JSONMedic

XML to JSON Converter

An XML to JSON converter reads an XML document and writes the same data as JSON. XML has attributes, repeated elements and mixed text that JSON does not have, so every converter applies mapping rules. Paste XML below to get JSON that follows RFC 8259; the rules this page uses are listed under the tool.

XML input
JSON output

Converted in your browser with its built-in XML parser · nothing is uploaded

Convert XML to JSON

Paste XML into the left box, click Open file, or drop an .xml file on it. The JSON updates as you type, with Copy and Download (data.json). The XML is parsed by your browser's built-in DOMParser and is not uploaded. There is no fixed size limit. In desktop Chrome a 1.1 MB XML file converted in about 1.6 seconds, an 11 MB file in about 6 seconds and a 29 MB file in about 17 seconds, during which the page does not respond. Input over 1 MB is not drawn in the input box, and the output pane draws the first 1 MB of JSON; Copy and Download always include all of it.

How XML maps to JSON

  • Root: the document element becomes the one top-level key: <order>…</order> → {"order": …}.
  • Attributes: keys with a prefix, @ by default ("@id"). Pick _ or - in Attribute prefix.
  • Text only: an element with just text becomes a string: <customer>Ada</customer> → "customer": "Ada".
  • Text plus attributes or children: the text goes under "#text".
  • Repeated elements: two or more siblings with the same name become an array. A single one stays a plain value, so <list><item>1</item></list> gives {"item": "1"} but two items give {"item": ["1", "2"]}.
  • Values: strings by default. Tick Numbers and true/false as JSON types to turn 12.50 into 12.5 and true into true; a value with a leading zero such as 02139 stays a string.
  • Empty elements: <gift/> becomes "", or an object with only its attributes.
  • Whitespace: text is trimmed, and whitespace between elements is dropped.
  • CDATA: treated as text. Comments and processing instructions are dropped.
  • Namespaces: names are kept as written, prefix included ("soap:Body"), and xmlns declarations stay as attributes ("@xmlns:soap").

Example: XML input and JSON output

This is the sample loaded in the tool, and the exact output with the default settings:

<?xml version="1.0" encoding="UTF-8"?>
<!-- a sample order -->
<order id="1042" status="paid">
  <customer>Ada Lovelace</customer>
  <item sku="A-1" qty="2">Notebook</item>
  <item sku="B-7" qty="1">Pencil</item>
  <note><![CDATA[Leave at the door & ring]]></note>
  <gift/>
</order>
{
  "order": {
    "@id": "1042",
    "@status": "paid",
    "customer": "Ada Lovelace",
    "item": [
      {
        "@sku": "A-1",
        "@qty": "2",
        "#text": "Notebook"
      },
      {
        "@sku": "B-7",
        "@qty": "1",
        "#text": "Pencil"
      }
    ],
    "note": "Leave at the door & ring",
    "gift": ""
  }
}

What does not survive conversion

  • Order across different names. JSON object keys hold no order between different elements: <a/><b/><a/> becomes one a array and one b.
  • Mixed content. <p>Hello <b>world</b> again</p> becomes {"b": "world", "#text": "Hello again"}: where the bold word sat in the sentence is lost.
  • Comments and processing instructions are dropped.
  • One or many. A single child is a value and two are an array, so code that reads the JSON should accept both.
  • Types. XML text has no types; with the type option off, every value is a string.

Validate and format the result

The output is generated by JSON.stringify, so it is always valid JSON, indented with 2 or 4 spaces. Under the output, View as a tree opens it in the JSON viewer and Check in the repair tool opens it in the repair tool (both for results up to 100 KB). From there: validate, format or minify it, query it with the JSONPath tester, or generate TypeScript types or a JSON Schema.

Fix XML errors that block conversion

Malformed XML gives no JSON. The page shows the parser's message with the line and column. These are Chrome's messages; other browsers word them differently:

ProblemXMLReported
Unclosed tag<a> <b>text </a>Line 3, column 5: Opening and ending tag mismatch: b line 2 and a
Tags closed in the wrong order<a><b></a></b>Line 1, column 11: Opening and ending tag mismatch: b line 1 and a
Bare ampersand<a>Tom & Jerry</a>Line 1, column 9: xmlParseEntityRef: no name (a literal & must be written &amp;)
Two root elements<a/> <b/>Line 2, column 1: Extra content at the end of the document
Unquoted attribute<a id=1/>Line 1, column 7: AttValue: " or ' expected

XML vs JSON: when to use each

JSON (RFC 8259) has six kinds of value (object, array, string, number, boolean, null) and nothing else, which maps directly onto the data structures of most programming languages; it is the usual format for web APIs. XML has elements, attributes, namespaces, mixed text and comments, so it suits documents and formats that are defined by a schema and need those features. When you only move data between programs, JSON is the simpler choice.

More on the formats in Learn JSON and the glossary; more converters under Convert and in the JSON converter. Errors in the JSON side are explained under JSON errors; all tools are in JSON Studio.

Frequently asked questions

Paste your XML into the input box, or open or drop an .xml file, and the JSON appears on the right. Attributes, repeated elements and text are mapped by the fixed rules described on this page. Copy it or click Download.

Yes. The output is written by the browser's JSON.stringify, so it is RFC 8259 JSON: objects, arrays and strings in double quotes, plus numbers and true/false when you turn on type conversion. You can check it with View as a tree.

JSON has no attributes, so each one becomes a key in the element's object, with a prefix: @ by default (id="7" becomes "@id": "7"). You can switch the prefix to _ or -.

Not on this page. The JSON converter writes XML from JSON (Convert tab, XML), using its own rules, so a round trip may not give back the same XML.

No. The page parses the XML with the browser's built-in DOMParser and builds the JSON in the page; the XML is not uploaded.

The JSON Medic newsletter

One JSON error and its fix, weekly, plus new guides and tools. Free.

We send a confirmation link first. Unsubscribe with one click in any mail. See the privacy policy.

Related JSON tools