How to Convert XML to JSON (Attributes, Arrays, Namespaces and CDATA Explained)
Convert XML to JSON and see exactly how attributes, repeated tags, text, namespaces and CDATA turn out, with a before and after example. Runs on your device.
Updated November 7, 20269 min read
To convert XML to JSON, paste the XML or drop the file into the XML to JSON converter, pick how you want the result shaped, press Convert to JSON and download or copy it. The part that trips people up is not the button, it is the mapping: XML has attributes, text, repeated tags, namespaces and CDATA, and JSON has only objects, arrays and plain values. This guide shows exactly what each XML feature becomes with the default settings, which options change it, and when a trip back to XML will not give you the same document.
Convert XML to JSON, step by step
Add your XML
Open the XML to JSON converter. Paste the markup into the editor or drop a file onto the page. It opens .xml, .rss, .atom, .svg, .kml, .gpx, .plist, .opml, .xsd, .wsdl, .xsl and a few more, and it detects the character encoding for you.
Choose the output shape
Output shape has five choices: Compact, BadgerFish, GData, Parker and JsonML. Compact is the default and the one most people expect. The mapping below uses Compact unless it says otherwise.
Set attributes, arrays and namespaces
Decide whether to keep attributes, what prefix their keys carry, whether a tag that appears once should still become an array, and what to do with namespace prefixes. The preview follows each change.
Convert and take the result
Press Convert to JSON, choose 2 spaces, 4 spaces, tabs or minified output under Indent, then download the .json file or copy it. If the file is not well-formed, you get the line, the column and the offending source line instead of broken JSON.
A before and after example
Here is one small document that uses most XML features at once, converted with the default settings (Compact shape, attribute prefix @_, text key #text, 2-space indent).
| XML in | JSON out |
|---|---|
<order id="A7" xmlns:s="urn:shop"> | "order": { "@_id": "A7", "@_xmlns:s": "urn:shop", ... } |
<s:item sku="007">Pen</s:item> and <s:item sku="008">Ink</s:item> | "s:item": [ { "@_sku": "007", "#text": "Pen" }, { "@_sku": "008", "#text": "Ink" } ] |
<note><![CDATA[a < b & c]]></note> | "note": "a < b & c" |
<gift/> | "gift": "" |
Read top to bottom, the rows are the children of one order object. Notice four things: the attributes carry the @_ prefix, the two s:item tags became an array, the CDATA text lost its wrapper and the sku 007 is still the string "007", not the number 7.
How each XML feature maps to JSON
| In the XML | In the JSON (Compact) | Why |
|---|---|---|
<a>text</a> | "a": "text" | An element with only text and no attributes becomes a plain value |
<a id="1">text</a> | "a": { "@_id": "1", "#text": "text" } | Attributes need an object, so the text moves under the text key |
<a/> | "a": "" | An empty element is an empty string unless you choose null or an empty object |
<i>1</i><i>2</i> | "i": ["1", "2"] | Same-named siblings become an array, in order |
One <i>1</i> alone | "i": "1" | A single occurrence stays a single value unless Arrays is set to Always |
<![CDATA[x < y]]> | merged into the text | CDATA is just text with the special characters left unescaped |
<p>Hello <b>x</b></p> | "p": { "b": "x", "#text": "Hello" } | Mixed content keeps both the child and the surrounding text |
<s:item> | "s:item" | The namespace prefix stays as written by default |
Attributes: the prefix and the text key
JSON has no attributes, so any converter needs a convention. This one writes each attribute as a key starting with @_ and puts element text under #text. The prefix exists so an attribute never collides with a child element of the same name: <book title="A"><title>B</title></book> would otherwise need two keys called title. You can change the Attribute prefix and the Text key, or turn off Keep attributes if you only want element content.
Two of the other shapes name things differently. BadgerFish uses @name for attributes and $ for text. GData writes attributes as plain keys and text as $t. Parker drops attributes altogether and also drops the root element's name, so you get just the content; that is its whole idea, and it is why the attribute controls do nothing in that shape.
Repeated elements and arrays
When a tag repeats among siblings it becomes an array, in the order it appeared. The catch is the single case. A feed with three <item> entries gives you an array, and the same feed with one entry gives you an object, so code that loops over the items breaks on the quiet day. If the JSON is going into code, set Arrays to Always: every element that can repeat becomes an array even when there is only one. The default, When repeated, gives the shorter, more readable output.
This matters most for RSS and Atom feeds: channel, item, category and atom:link are all repeatable. Feed text often arrives inside CDATA, for example content:encoded, and ends up as ordinary string values.
Namespaces: keep, strip or expand
| Setting | <soap:Body> becomes | Use it when |
|---|---|---|
| Keep prefix (default) | "soap:Body" | You are reading SOAP or WSDL and want the JSON to match the document |
| Strip | "Body" | You want short keys and your document has no clashing names |
| Expand to URI | "{http://schemas.xmlsoap.org/soap/envelope/}Body" | The JSON will be compared or validated, so nothing may be ambiguous |
Strip is the only one that can lose information. If a:id and b:id both become id, they merge into one key as if they were repeats of the same element. The tool counts these collisions and tells you, rather than letting the merge pass quietly. A prefix used without being declared is an error, because guessing a namespace would put made-up data in your output.
All five shapes, namespace handling and type options are free. Free: one file up to 25 MB at a time. Pro: up to 50 files and a single ZIP download. Runs on your device, not our servers.
Convert XML to JSONIs the round trip back to XML lossless?
Mostly no, and it depends on the shape. Every key-based shape (Compact, BadgerFish, GData, Parker) stores children by name in an object, and an object has no order between different keys. Take <a>1</a><b>2</b><a>3</a>. Compact gives "a": ["1", "3"], "b": "2", and nothing in that says the b sat between the two a elements. Mixed content has the same problem: in <p>Hello <b>x</b> there</p> the text is gathered under #text, so where the bold part sat is gone. Comments and processing instructions are dropped by default, so turn on Keep comments or Keep processing instructions if you need them.
The one exception is JsonML. It writes every element as an array of its name, an optional attribute object and its children in order, so <a>1</a><b>2</b><a>3</a> inside a root becomes ["root", ["a", "1"], ["b", "2"], ["a", "3"]]. Nothing is reordered. It is more awkward to read, but it is the shape to pick when the document must come back exactly.
When order does not matter, going back with JSON to XML works well, with one setting to know. Its option for turning @ keys into attributes is off by default, and it removes only the leading @ from a key. Since XML to JSON writes @_id by default, set the Attribute prefix to @ before converting, or you will get an attribute called _id on the way back. Two more things do not survive: a number you inferred from text loses its original spelling, and a single-item array comes back as a lone element.
Empty elements, whitespace and CDATA
Empty elements can become an empty string, null or an empty object. Pick null if a missing value and a blank value mean different things downstream, such as a nillable field. Whitespace is set to Trim by default, so the indentation inside a pretty-printed file does not leak into your strings. Collapse reduces runs of spaces and line breaks to a single space, and Keep as written leaves the text exactly as it is.
Merge CDATA into text is on by default, which is usually what you want, since CDATA is only a way to write characters like < and & without escaping them. Turn it off and each CDATA section is kept under its own #cdata key instead. In GData and Parker the option is hidden, because those shapes have no key to put a separate section under.
Limits, formats and errors
| Free | Pro | |
|---|---|---|
| File size | 25 MB per file | No cap imposed by the tool, only what your device can handle |
| Files at once | 1 | Up to 50, delivered as one ZIP |
| Conversion options | All of them | All of them |
| Extra outputs | JSON | Also one record per line (NDJSON) and a JSON Schema |
Input can be UTF-8, UTF-16, Windows-1252 or ISO-8859-1, and Encoding is set to Detect by default. The parser is strict on purpose. An unmatched end tag, a bare &, a duplicate attribute or an unknown entity such as stops the conversion with a message naming the line and column. If you are sure the file is fine apart from unknown entities, turn on Allow unknown entities and it will go through. For a deeper look at choosing between the formats, see JSON vs CSV vs XML, and once you have the JSON, the JSON formatter and JSON validator help you check it.
Frequently asked questions
How do I convert XML to JSON online?
Paste the XML or drop the file into the XML to JSON converter, choose an output shape (Compact is the default), press Convert to JSON, then download or copy the result. Free handles one file up to 25 MB at a time, and it runs on your device.
What happens to XML attributes in JSON?
By default each attribute becomes a key starting with @_, such as "@_id": "A7", and element text sits under #text when the element also has attributes. You can change the prefix and text key or turn attributes off. Parker is the one shape that always drops them.
Why is one XML element an object but another is an array?
A tag that repeats among siblings becomes an array and a tag that appears once stays a single value. If your code expects an array every time, set Arrays to Always so even a single element is wrapped in one.
How do I convert an RSS feed to JSON?
Paste the feed or drop the .rss or .xml file into the converter. Each item becomes an entry in an array, atom: and content: prefixes are kept by default, and CDATA text turns into normal strings. Set Arrays to Always if the feed may have just one item.
Can I convert the JSON back to the same XML?
Only if nothing depends on order. Compact and the other key-based shapes lose the order of different tags and the position of text between elements. JsonML keeps document order exactly. When going back with JSON to XML, set the attribute prefix to @ so attributes map back correctly.
Why does my XML show a parse error?
The converter is strict. Common causes are an unclosed or mismatched tag, an unescaped ampersand (use &), a duplicate attribute, an undeclared namespace prefix or an unknown entity like . The message gives the line, the column and the source line so you can fix it.