XML to JSON Converter
Converts XML to JSON with attributes, repeated elements, CDATA and namespaces handled. You choose the attribute prefix, the text key and when arrays are used, and errors point to the line.
- Runs in your browser
- Nothing uploaded
- Free, no sign-up
How to Use XML to JSON Converter
- Paste XML or open an .xml, .rss, .svg or .kml file. Click Sample to see a catalog with attributes, CDATA and a namespace.
- Pick the attribute prefix (@, _ or none) and the key used for element text.
- Choose "Always use arrays" if your code needs the same shape for one item or many, and turn on number conversion if you want 9.50 as a number.
- If the XML is broken, the error shows the line and a hint. For a bare & or an HTML entity such as , click the fix button.
- Copy the result, or save it with Download .json.
Example: a book catalog with attributes, repeats and CDATA
The Sample button loads this XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:media="http://example.com/media">
<!-- two books, one with a single author -->
<book id="bk101" lang="en">
<title>XML Developer's Guide</title>
<author>Gambardella, Matthew</author>
<author>Knorr, Stefan</author>
<price currency="USD">44.95</price>
<media:cover>cover-101.jpg</media:cover>
<description><![CDATA[Covers <xml> & XSLT basics.]]></description>
</book>
<book id="bk102">
<title>Midnight Rain</title>
<author>Ralls, Kim</author>
<price currency="USD">5.95</price>
<instock/>
</book>
</catalog>
The first book in the result looks like this:
{
"@id": "bk101",
"@lang": "en",
"title": "XML Developer's Guide",
"author": [
"Gambardella, Matthew",
"Knorr, Stefan"
],
"price": {
"@currency": "USD",
"#text": "44.95"
},
"media:cover": "cover-101.jpg",
"description": "Covers <xml> & XSLT basics."
}
Attributes got the @ prefix, the two author elements became an array, and price, which has both an attribute and text, became an object with #text. The CDATA section is plain text in JSON, the comment is gone, and the namespaced media:cover kept its prefix. The second book has only one author, so there author is a single string. The note under the result points this out, and “Always use arrays” changes it.
How XML maps to JSON
There is no single standard for this mapping, so the options matter. Each row below was produced by the tool, with the option named in the first column:
| Case | XML | JSON |
|---|---|---|
| Element with text | <name>Ada</name> |
{"name":"Ada"} |
| Attribute | <user id="7">Ada</user> |
{"user":{"@id":"7","#text":"Ada"}} |
| Repeated elements | <tags><tag>a</tag><tag>b</tag></tags> |
{"tags":{"tag":["a","b"]}} |
| Single element | <tags><tag>a</tag></tags> |
{"tags":{"tag":"a"}} |
| Single element, Always use arrays | <tags><tag>a</tag></tags> |
{"tags":{"tag":["a"]}} |
| Empty element | <user><phone/></user> |
{"user":{"phone":""}} |
| Empty element as null | <user><phone/></user> |
{"user":{"phone":null}} |
| CDATA | <code><![CDATA[a < b && c]]></code> |
{"code":"a < b && c"} |
| Namespaces | <s:Envelope xmlns:s="urn:x"><s:Body>1</s:Body></s:Envelope> |
{"s:Envelope":{"@xmlns:s":"urn:x","s:Body":"1"}} |
| Namespaces, prefixes off | <s:Envelope xmlns:s="urn:x"><s:Body>1</s:Body></s:Envelope> |
{"Envelope":{"Body":"1"}} |
| Mixed text and elements | <p>Hello <b>world</b> again</p> |
{"p":{"b":"world","#text":"Hello again"}} |
| Numbers as text (default) | <item><qty>007</qty><price>9.50</price><ok>true</ok></item> |
{"item":{"qty":"007","price":"9.50","ok":"true"}} |
| Numbers converted | <item><qty>007</qty><price>9.50</price><ok>true</ok></item> |
{"item":{"qty":"007","price":9.5,"ok":true}} |
How it works
- The XML is parsed by your browser’s built-in XML parser, the one it also uses for SVG and other XML files. It is strict, so a document that converts here is well-formed.
- When parsing fails, the tool shows the line and column the browser reports, the line with a marker, and a hint. Before parsing it also scans for a bare
&and for HTML entities such as oré, which XML does not define. The fix button rewrites them as&and numeric references like . - Entities and character references such as
<andéare decoded into the characters they stand for. - Values stay text unless you turn on number conversion. Even then, values with a leading zero such as 007 and integers longer than 15 digits stay text, so IDs and codes are not damaged.
- With prefixes kept, the
xmlnsdeclarations come along as attributes. Turn prefixes off to get plain names likeBodyinstead ofsoap:Body; the declarations are then left out.
Limits
- Comments, processing instructions and the DOCTYPE are dropped.
- When an element mixes text and child elements, as in HTML-like content, the text is joined under the text key and its position between the children is lost.
- Without “Always use arrays”, the shape depends on the data: one child gives a value, two give an array. Code that reads the JSON should expect both, or the option should be on.
- Schemas (XSD) are not read, so types come only from the number conversion option.
Frequently asked questions
Why are some elements arrays and others not?
An element that appears once becomes a single value, and one that repeats becomes an array. That keeps the JSON small, but the shape changes with the data. Turn on “Always use arrays” to wrap every child element in an array.
How do I convert an RSS feed to JSON?
Paste the feed XML or open the saved .rss file. Each item element becomes an entry in rss.channel.item, and attributes such as version=”2.0″ get the @ prefix. Hand-edited feeds sometimes contain a bare & or that strict XML rejects; the fix button handles those.
What happens to namespaces like soap: or xsi:?
By default the prefix stays part of the key, for example “soap:Body”, and the xmlns declarations are kept as attributes. Turn off “Keep namespace prefixes” to drop both and get shorter keys.
Can I turn the JSON back into XML?
Use the JSON to XML converter. It reads the same @ attribute and #text conventions, so the round trip keeps attributes and text in place.
Related Tools
JSON to XML Converter →
Converts JSON to well-formed XML. Keys starting with @ become attributes, arrays become repeated…
Convert CSV to JSON →
Converts CSV or TSV to JSON in your browser. It detects the separator, handles…
Convert CSV to XML →
Convert CSV to XML with custom tag names, element/attribute modes, CDATA wrap, headerless mode.…
JSON to CSV Converter →
Turns a JSON array into CSV or TSV. Nested fields become their own columns,…
Convert JSON to PDF Report →
Convert JSON data to formatted PDF reports instantly. Free JSON to PDF converter with…
JSON to YAML Converter →
Turns JSON into clean, readable YAML. Strings that YAML would misread, such as NO,…
XML Formatter →
Formats XML with 2 spaces, 4 spaces or tabs, or minifies it to one…
Convert XML to PDF Invoice →
Convert XML invoice data to professional PDF invoices instantly. Support for UBL and custom…
XML to YAML Converter →
Convert XML into readable YAML that mirrors the document tree, with indentation for nesting.
YAML to JSON Converter →
Converts YAML to JSON with anchors, aliases and
Analyze JSON Online and Validate JSON Structure →
Analyze JSON Online Validate & Analyze JSON Structure with detailed statistics including depth, key…
Android AssetLinks JSON Generator - App Links →
Android AssetLinks JSON Generator files for Android App Links. Configure package names and SHA-256…