Skip to content
StringToJSON

XML → JSON

Convert an XML string to JSON.

Paste a document, a SOAP body or an RSS fragment and get a JSON tree that keeps the attributes, the repeated nodes and the text where you can find them.

XML

0 B

JSON

0 B

Output appears here.

Ready. Paste something above.

Everything runs locally in your browser — your data never leaves this page. Press⌘/Ctrl +Enter to run.

What the conversion actually does

XML and JSON model data differently, so no conversion is entirely lossless. XML has attributes, ordered children, namespaces, comments and mixed content; JSON has objects, arrays and four scalar types. Every converter has to pick a set of conventions, and the useful thing is knowing which ones are in play.

The rules used here

  • The root element becomes the single top-level key, so the document keeps its name.
  • Attributes become keys prefixed with @.
  • An element with no attributes and no children becomes its trimmed text content.
  • Two or more siblings sharing a tag name collapse into an array.
  • Mixed text alongside child elements is stored under #text.
  • CDATA sections are read as ordinary text.
  • Values stay strings — nothing is coerced to a number or boolean.

Example

<!-- input -->
<order id="A-1093">
  <item sku="TEA-01">2</item>
  <item sku="MUG-07">1</item>
</order>

// output
{
  "order": {
    "@id": "A-1093",
    "item": [
      { "@sku": "TEA-01", "#text": "2" },
      { "@sku": "MUG-07", "#text": "1" }
    ]
  }
}

When the parse fails

XML is far stricter than HTML. Every tag must close, attribute values must be quoted, and the five reserved characters — &, <, >," and ' — must be written as entities inside text. A bare ampersand in a URL is the single most common cause of a failed parse. Unclosed tags and a stray byte-order mark before the declaration come next.

When the document is not well-formed, the browser's parser error is reported directly rather than being swallowed, so you can see which construct it objected to.

Doing this in your own code

Most languages need a library, because none of them ship an XML-to-JSON bridge in the standard library:

  • JavaScript / Nodefast-xml-parser or xml2js. In the browser, DOMParser plus a short walk (what this page does) avoids a dependency.
  • Pythonxmltodict.parse(xml) returns a dict that json.dumps serializes directly.
  • Javaorg.json.XML.toJSONObject(xmlString) is a one-liner; Jackson's XmlMapper handles larger jobs.
  • C#JsonConvert.SerializeXNode in Newtonsoft.Json.

Each library picks slightly different conventions for attributes and single-element arrays, so compare their output against what your consumer expects before committing. Once you have JSON, the formatter and validator take it from there.

What the conversion cannot carry across

Three things have no JSON equivalent and are dropped or flattened. Namespacessurvive only as part of the tag name — soap:Envelope stays a literal key, so prefixes that vary between responses will produce keys that vary with them. Comments and processing instructions are discarded, including the XML declaration. Anddocument order between different tag names is preserved only by accident: JSON object keys are conventionally unordered, so a consumer is entitled to reorder them.

That last point is the one that causes real bugs. If your XML is a sequence where order carries meaning — a list of steps, a transaction log — and the entries have different tag names, the JSON representation cannot express that ordering. The fix is to restructure into an array before the order is lost, which usually means changing the XML rather than the converter.

Where XML still shows up

Plenty of places, despite JSON having won the API argument: SOAP services in finance and logistics, RSS and Atom feeds, SAML assertions, Office and OpenDocument file formats, Android layouts, Maven builds and sitemaps. Most of the time you are not choosing XML — you are consuming something that already made the choice years ago, and converting it is the shortest path to code that is pleasant to write.

Frequently asked questions

How do I convert an XML string to JSON?
Paste the XML into the left pane. It is parsed with the browser's built-in DOMParser and walked into a JSON tree — attributes become @name keys, element text becomes the value, and repeated sibling tags collapse into an array.
Why are some keys prefixed with @?
XML has two ways to attach data to an element — attributes and child elements — and JSON has only keys. Prefixing attributes with @ keeps them distinguishable from children of the same name. This is the same convention Badgerfish and most XML-to-JSON libraries use.
What is the #text key for?
It appears when an element holds both text and child elements — so-called mixed content, common in HTML-like markup. The text needs somewhere to live that cannot collide with a child tag name.
Why is a single repeated element not an array?
Without a schema, a converter cannot tell whether one <item> means "one item" or "a list that happens to have one entry". Two or more siblings produce an array; one produces an object. If your consumer needs a consistent shape, normalize it after conversion.
Are numbers and booleans converted?
No. Every XML text node is a string, and guessing types silently corrupts data — a zip code such as 01234 would lose its leading zero. Values stay strings; cast them deliberately on the other side.