Home Tools Blog About

XML to JSON Converter

In short

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
Runs in your browser; the XML you paste or open is not uploaded.
🛡
Private by designYour files and text stay in this tab
⚡
Runs in your browserNo waiting for a server
💧
No watermarksClean output you can use
🆓
FreeNo account needed

How to Use XML to JSON Converter

XML to JSON Converter · free online tool · All Tools Verse
The XML to JSON Converter tool, as it looks in your browser.
  1. Paste XML or open an .xml, .rss, .svg or .kml file. Click Sample to see a catalog with attributes, CDATA and a namespace.
  2. Pick the attribute prefix (@, _ or none) and the key used for element text.
  3. 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.
  4. 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.
  5. 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 &nbsp; or &eacute;, which XML does not define. The fix button rewrites them as &amp; and numeric references like &#160;.
  • Entities and character references such as &lt; and &#233; 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 xmlns declarations come along as attributes. Turn prefixes off to get plain names like Body instead of soap: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 &nbsp; 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.

Keep going

Related Tools

All Document tools →
Share

Embed this tool

Add this free tool to your website. Copy and paste the code: