XML to JSON

Convert XML to JSON, preserving attributes and namespaces.

XML Input
JSON Output
JSON output appears here

Related Tools

Documentation

What is XML to JSON?

XML to JSON converts an XML document into an equivalent JSON text file with syntax highlighting, ready to copy or download — unlike XML Parser's inspection-only tree, this tool produces an actual JSON output you can paste into other tools or code.

How it works

The XML is parsed with the browser's native DOMParser. If parsing produces a parsererror node, the tool throws with that message, extracts a line number when the message mentions one, and highlights that line in the input gutter. Otherwise the root element is converted recursively: attributes become @-prefixed keys, elements with no children collapse to their text content, elements with both attributes and text keep the text under a #text key, and two or more sibling elements sharing a tag name are grouped into a JSON array instead of overwriting each other. The resulting object is serialized with JSON.stringify(result, null, 2) and shown with JSON syntax highlighting.

Features

  • Attributes preserved as @-prefixed keys
  • Repeated sibling tags become JSON arrays
  • Mixed text/child content preserved under a #text key
  • Syntax-highlighted JSON output panel
  • Copy, download as .json, or load an .xml file directly

Example

Input: <root><name>DevFormats</name><tools><tool id="1">json-formatter</tool><tool id="2">yaml-formatter</tool></tools></root>

Output:

{
  "root": {
    "name": "DevFormats",
    "tools": {
      "tool": [
        { "@id": "1", "#text": "json-formatter" },
        { "@id": "2", "#text": "yaml-formatter" }
      ]
    }
  }
}

Common errors

An unclosed tag or more than one root element produces "Malformed XML" (or the browser's underlying parsererror text) instead of output. Because the conversion has no schema awareness, an element that appears once in your sample but could repeat elsewhere will convert to a single object rather than a single-item array — check consuming code handles both shapes if the source data is inconsistent.

Best practices

Decide up front how your consuming code should treat attributes — the @-prefix convention used here is common but not universal, so confirm any downstream library (e.g. a specific XML-JSON mapping spec) expects the same shape before wiring this output directly into a pipeline.

Frequently Asked Questions

How are XML attributes represented?

Each attribute becomes a key prefixed with @, e.g. becomes {"@id": "1"}, so attributes and child elements never collide by name.

What happens with repeated sibling tags?

They become a JSON array — two or more siblings under the same parent produce a "tool": [...] array instead of overwriting each other.

What about mixed text and child elements?

Direct text content alongside child elements is captured under a "#text" key so no data is silently dropped.

Is my data uploaded anywhere?

No — the conversion runs entirely in your browser via the native DOMParser.