XML to JSON Converter

Paste XML to get JSON, or paste JSON to get XML. Attributes, repeated elements and namespaces are handled.

Paste your XML or JSON

Converted XML to JSON.

How to convert XML to JSON

  1. Paste your XML into the XML box, or press Load sample. The JSON box fills in as you type.
  2. Pick the JSON indent. Tick Convert numbers and true/false if you want typed values. Left unticked, every value stays a string.
  3. Press Copy or Download .json to take the result.
  4. To go the other way, paste JSON into the JSON box and the XML box updates. A mistake in either box shows the line and column in the message under the boxes.

How XML maps to JSON

Your browser's built-in DOMParser reads the XML, and the page builds JSON with these rules:

Worked example

With the sample loaded, the two <user> elements repeat, so they become an array, and the id attributes become @id keys. The tool shows:

{
  "users": {
    "user": [
      {
        "@id": "1",
        "name": "Alice",
        "email": "alice@example.com"
      },
      {
        "@id": "2",
        "name": "Bob",
        "email": "bob@example.com"
      }
    ]
  }
}

Pressing Convert to XML on that JSON rebuilds both <user id="..."> elements, with an XML declaration on the first line.

JSON to XML rules

If the JSON object has exactly one top-level key, that key becomes the root element. Otherwise the output is wrapped in <root>, and a top-level array becomes repeated <item> elements. Keys starting with @ become attributes, #text becomes text, arrays repeat their tag, and null becomes an empty element. Special characters are escaped, and a key that is not a valid XML name, such as one with a space, gets underscores instead.

Limits and edge cases

XML has no array type, so one <user> gives an object while two give an array. Code that reads the JSON should handle both. Surrounding whitespace is trimmed, and mixed text and elements are joined into one #text value, so their order is lost. Values such as 007 or 1.50 stay strings even when the number option is on, so no digits are lost. Internal DOCTYPE entities are expanded and external ones are never fetched. Your text is parsed in your browser and is not uploaded. See also YAML to JSON, CSV to JSON, the XML formatter and the JSON formatter.

Frequently asked questions

How are XML attributes converted to JSON?

Each attribute becomes a key that starts with @, so <user id="1"> gives "@id": "1". If the element also has text, the text goes in a "#text" key.

Why is one child an object but several become an array?

XML has no array type, so only elements that repeat under the same parent become an array. One <user> gives an object and two give an array.

Are numbers and booleans converted?

Not by default: XML text is just text, so every value stays a string. Tick Convert numbers and true/false to turn values like 42, 3.5 and true into JSON types.

What happens to comments and namespaces?

Comments and processing instructions are dropped. Namespace prefixes stay in the key names, such as soap:Body, and xmlns declarations appear as keys like @xmlns:soap.

Can I convert JSON back to XML without losing data?

Mostly. Attributes, text and repeated elements survive the round trip. Comments, the order of mixed text and elements, and whether a one-item list was an array are not kept.

Sources: W3C Extensible Markup Language (XML) 1.0, Namespaces in XML 1.0, RFC 8259 (JSON), and MDN: DOMParser.