toolbench.onlineby Valkyron Labs

Converters

XML to JSON

Convert XML into readable JSON so nested tags, attributes, and repeated elements are easy to scan. Copy or download the two-space result from your browser.

XML to JSON

Convert XML documents into readable JSON.

Paste XML to convert it into readable JSON.

JSON output

—

Output actions

Ready to copy output once your result appears.

One object, @ attributes, and arrays for repeats

XML to JSON parses one document and prints a 2-space JSON object whose single key is the root tag. A leaf with no attributes is a string. Attributes are keys that start with @. Text that shares an element with attributes or with child elements is stored under #text. A second child with the same tag turns that key into an array. Comments, processing instructions, and CDATA are not copied into the object. Copy output and Download output use the JSON text, and the file is named xml-to-json.txt.

How to convert XML to JSON

  1. 1Paste a single document into XML input. The placeholder is an item element whose only child is a title. The box starts empty.
  2. 2Press Convert to JSON. There is no mode menu and no checkbox. The conversion runs only on that click.
  3. 3Read JSON output. Success says XML converted to JSON. A parse problem clears the pane and shows the parser error text. Copy output copies the JSON. Download output saves xml-to-json.txt, not a .json file.

What it is good for

  • • Seeing a short element tree as an object you can scan without the angle brackets.
  • • Noticing that two sibling tags with one name became an array, while one sibling stayed a string or an object.
  • • Reading an attribute as an @ key and the element's text as #text.

Example

Input

<item><title>Toolbench</title></item>

Output

{
  "item": {
    "title": "Toolbench"
  }
}

The root key is item. title has no attributes and no child elements, so its value is the string Toolbench rather than an object.

How a node becomes a JSON value

Convert to JSON parses the paste, then walks the root element. The printed value is an object with one property, and the property name is the root's tag name, including any prefix the parser kept on that name. Inside an element, the walk looks at attributes, element children, and text. If the element has neither attributes nor element children, the JSON value is the trimmed text, which may be an empty string. That is why a title leaf is "Toolbench" and why <empty/> becomes "" under its parent key.

If the element has an attribute or an element child, the value is an object. Each attribute is a key formed by @ plus the attribute name, and the attribute value is decoded, so a source &amp; is the character & in the JSON string. Direct text, after each text node is trimmed and the pieces are joined, is stored as #text when it is not empty. Element children are then added in order. The first time a tag name appears, the value is that child. The second time, the existing value and the new child are wrapped in an array. A later sibling with the same name is pushed onto that array. Siblings with different names stay separate keys.

The Convert button and the result

There is one button, Convert to JSON, and one box. Nothing reruns while you type. The status starts as Paste XML to convert it into readable JSON. A good click replaces JSON output with JSON.stringify of the object at two-space indent and says XML converted to JSON. The indent of the JSON is fixed. You cannot ask for a minified object or for four spaces from this page.

Copy output copies that JSON text. Download output writes xml-to-json.txt because the shared download control does not treat this slug as a JSON file. Rename it if the next tool expects .json. A failed parse sets the pane to empty before the status updates, so the previous object is not left behind to copy by mistake.

When a JSON view is the right view

Use this page when you want to see nesting, repeats, and attributes without reading brackets. A sitemap or a feed that you only want reindented should stay on the XML formatter, which keeps comments and the serialized tags. This page drops comments. After you have the object, the JSON formatter can reindent it again, and JSON compare can diff two of those objects. Neither of those tools parses XML.

YAML is a different conversion. The YAML and JSON converter reads JSON or YAML, not an XML document. Paste the object from this page into that converter if you want a YAML mapping. Do not paste the original tags there.

Repeats, comments, CDATA, and mixed text

Two <to> children under one parent become an array of strings when those children are leaves. The order is the order in the document. One <to> child stays a single string, not a one-element array. You cannot force every child into an array. Attributes on a repeated child make each array entry an object: @id and, when the element also has text, #text. A leaf with only text never gets a #text key.

Comments and processing instructions are omitted. They are not element children and they are not collected as text. CDATA is also omitted, because the walk only reads text nodes. A root whose only content is a CDATA section therefore becomes an empty string, even though the XML formatter would still show the CDATA block. Mixed content trims each text node before joining them, so the spaces around an inline child disappear: Hello, a space, an inline element, a space, and an exclamation mark become the #text value Hello! beside the child key. Whitespace-only text is dropped when the element also has children or attributes. A document that does not parse fails the same way as the formatter: the pane is cleared and the status is the parser error text. There is no repair and no second root.

Rules and limitations

The output is one JSON object keyed by the root tag, pretty-printed with two spaces. Leaf text is a string. Attributes use an @ prefix. Shared text uses #text. The second sibling with a repeated tag starts an array.

Comments, processing instructions, and CDATA are not copied. A parse error clears the pane. Download output saves xml-to-json.txt. There is no setting for prefixes, arrays, or indent.

Common problems

If a repeated tag is an object on the first row and an array after you add a sibling, that is the one-then-many rule. The page will not wrap a single child in brackets.

If a CDATA payload became an empty string, the walk never read that node. Use the formatter when you need the CDATA bytes preserved. If spaces around an inline tag vanished, each text node was trimmed before the join.

Frequently asked questions

Why do attributes start with @?

The @ marks an attribute so it cannot collide with a child tag of the same name. The rest of the key is the attribute name, and the value is decoded text.

When does a tag become an array?

When a second element child uses that same tag name. One child stays a string or an object. The array keeps document order.

Are comments and CDATA included?

No. Comments and processing instructions are skipped. CDATA is not a text node in this walk, so a CDATA-only element becomes an empty string.

What file is downloaded?

xml-to-json.txt. The contents are the pretty-printed JSON. The name does not end in .json. Rename the file if you need that suffix.

Is my input uploaded or saved?

This tool processes input in your browser and does not save its input or output to local storage. Copy and download are user actions. The site uses analytics for page and action metadata and may use ads and production error reporting; see the Privacy Policy for those services.

About this tool

Maintained by Toolbench. For large or deeply nested files, split the input into smaller batches and verify representative results before using them in an import or application. Report reproducible problems through Contact.

Next steps

Related tools

Continue the workflow with nearby tools for formatting, publishing, debugging, or follow-up checks.

Trust

Policies, contact, and answers

Clear pages for how Toolbench works, what we collect, and how to reach us.