200 tools

Code & JSON Tools · · 4 min read

How to Convert XML to JSON Online

Use the XML to JSON converter to turn XML documents into JSON, with attributes becoming @keys and repeated tags becoming arrays automatically.

To convert XML to JSON, open XML to JSON, paste your XML into the input, and the converted JSON appears instantly — attributes become @-prefixed keys, repeated child tags become arrays, and nothing leaves your browser.

What this tool does and why it matters

XML and JSON model data differently: XML has attributes, mixed text-and-element content, and no built-in idea of "this is a list" — a tag either appears once or several times. Writing a converter by hand means deciding how to represent all three of those in JSON. Toolbench's XML to JSON tool makes consistent choices for you: element attributes become keys prefixed with @, leftover text inside an element becomes a #text key, and any child tag that appears more than once automatically becomes a JSON array — so you don't have to guess whether your code should expect an object or a list.

How to convert XML to JSON with Toolbench

  1. Open XML to JSON.
  2. Paste your XML into the input panel.
  3. Leave Detect numbers & booleans on (the default) so attribute and text values like 7, 98.5, true and false become real JSON types instead of quoted strings.
  4. Leave Always use arrays for children off (the default) to get a plain object when a child tag appears only once, or turn it on to always wrap children in an array, even when there's just one.
  5. Pick Indent: 2 spaces (default), 4 spaces, or Tab.
  6. Copy the resulting JSON.

Example: a single child of each tag (the defaults)

With Detect numbers & booleans on and Always use arrays for children off, this input:

<user id="7"><name>Ana</name><role>Designer</role></user>

becomes:

{
  "user": {
    "@id": 7,
    "name": "Ana",
    "role": "Designer"
  }
}

The id attribute becomes @id, converted to a real number. Each child element that appears only once becomes a plain key — name and role are strings, not arrays, since neither value looks like a number, true, false or null.

Example: repeated tags become arrays automatically

Repeat a child tag and the tool switches to an array for that key — regardless of the Always use arrays for children setting, which only matters when there's a single match. Given:

<cart>
  <item sku="A1">Mug</item>
  <item sku="A2">Pen</item>
</cart>

the output is:

{
  "cart": {
    "item": [
      {
        "@sku": "A1",
        "#text": "Mug"
      },
      {
        "@sku": "A2",
        "#text": "Pen"
      }
    ]
  }
}

Each <item> has both an attribute and text content, so it becomes an object with @sku and #text rather than a plain string. Because <item> appears twice, item is an array even though Always use arrays for children is off.

What each option changes

Option On Off
Detect numbers & booleans Attribute and text values matching a plain number, true, false or null become real JSON types Every attribute and text value stays a quoted string, exactly as written
Always use arrays for children Every child tag becomes an array, even if it only appears once A child tag that appears once becomes a plain key; only repeated tags become arrays
Indent 2 spaces (default) or 4 spaces for readable output Tab for output meant to be minified or diffed more compactly

Tips and common mistakes

  • Attributes always get an @ prefix. <user id="7"> becomes "@id": 7 — this avoids clashing with a child element that happens to share the same name as an attribute.
  • Mixed text and attributes produce a #text key. An element with both an attribute and direct text content, like <item sku="A1">Mug</item> above, becomes an object with @sku and #text rather than collapsing to a plain string.
  • A plain element with no attributes and no children becomes its text value. <role>Designer</role> by itself becomes the string "Designer", not an object — the tool only builds an object when there are attributes or child elements to hold.
  • Type detection is strict about format. Only values that look exactly like a plain integer, decimal, true, false or null convert; anything else, including things like 007 or 1,000, stays a string.
  • Invalid XML throws a clear error instead of guessing. If your document isn't well-formed (an unclosed tag, for example), the tool reports the parser error rather than returning partial or incorrect JSON.
  • JSON to XML — convert JSON back into well-formed XML.
  • CSV to JSON — convert spreadsheet rows into a JSON array.
  • JSON Formatter — pretty-print, minify or sort keys in a JSON document.
  • JSON Validator — check JSON and pinpoint syntax errors by line and column.

Frequently asked questions

Does this tool send my XML data to a server?

No. Parsing and conversion happen entirely in your browser. If you're signed in, your input syncs privately to your account; if you're signed out, nothing is saved anywhere.

Why does my JSON have @ keys?

Those are XML attributes. <user id="7"> becomes "@id": 7 inside the user object — the @ prefix keeps attributes from colliding with child element names.

Why did one tag come out as an array and another didn't?

A child tag becomes an array only when it appears more than once under the same parent, or when Always use arrays for children is turned on. A tag that appears just once becomes a plain key by default.

What happens to text mixed with attributes?

If an element has an attribute and its own text content, the text is stored under a #text key alongside the @-prefixed attributes, instead of being dropped.

What if my XML isn't well-formed?

The tool parses your document and reports a parser error instead of guessing — fix the reported issue, such as an unclosed or mismatched tag, and convert again.

More guides

All guides →