StringMash.com

XML to JSON

Read XML into JSON, with a rule for attributes that you can see.

Conversion
69 characters
Updates as you type
Attributes prefixed with @

Using the converter

Paste XML on the left and JSON appears on the right. Attributes keeps or drops them, and dropping them is right more often than you would think for XML that only uses attributes for ids.

An element holding nothing but text becomes a string. An element with attributes or child elements becomes an object, where attributes are prefixed with @ and any loose text sits under #text. Two children with the same name become an array.

There is no correct mapping

XML and JSON disagree about what a document is. XML has attributes, JSON does not. XML lets text and elements sit side by side inside one tag, and JSON has no way to hold that. XML has a single root, namespaces, comments and processing instructions, and JSON has none of them.

So every XML-to-JSON converter invents a convention, and there are at least half a dozen named ones: Badgerfish, Parker, GData, Spark and more. They disagree about attribute prefixes, about whether a single child should be an array, and about what to do with mixed content.

This page uses the @ and #text convention because it loses the least and reads the clearest. The important thing is knowing which one you have, since JSON produced under one set of rules will not go back through another.

Why XML got smaller and JSON got bigger

XML 1.0 became a W3C Recommendation in February 1998 and spent a decade as the default answer for everything: configuration, documents, remote calls, feeds. SOAP wrapped every message in envelopes of it.

JSON went the other way. Douglas Crockford has said he discovered rather than invented it, in 2001, since it is a subset of JavaScript that was already there. The grammar fits on one page, and there is nothing to argue about because there is nothing in it.

XML has not gone anywhere. It is still under RSS and Atom feeds, Office documents, SVG, Android layouts and most enterprise messaging, which is why this converter exists.

Related conversions

JSON to XML is the return trip and has to invent a root element. YAML to JSON covers the format that replaced XML for configuration.

The same tag parser drives HTML to Markdown, pointed at prose instead of data.

Questions

How do I convert XML to JSON?

Paste the XML above. Attributes are kept with an @ prefix by default, and you can drop them with the Attributes control.

Why do attributes have an @ in front?

To keep them apart from child elements, which would otherwise collide. An element with an id attribute and an id child would produce one key without the prefix.

What happens to repeated elements?

The second one turns the value into an array. A single child stays a plain value, so XML with one item reads differently from XML with two.

Is there a standard for this?

No. Several named conventions exist, including Badgerfish and Parker, and they disagree. This page uses the @ and #text convention and says so, so you know what you have.