OnlineTextLab
Esc

XML to JSON Converter

A strict XML reader that keeps attributes, CDATA and namespaces, and stops at the exact character when a document is broken.

JSON output

Your JSON appears here as you type.

Nothing to paste yet? Use Try an example to see it work.

Options
Attributes

Runs on your device. Your files and text are not uploaded.

About XML to JSON

XML and JSON do not map cleanly onto each other. XML has attributes, namespaces, mixed text and repeated elements, while JSON has objects and lists. This converter makes those choices visible and lets you change them, so the output fits whatever is going to read it.

It is useful when a legacy system, SOAP service, RSS feed or configuration file hands you XML and the rest of your stack wants JSON. Because the reader is strict, it doubles as a quick way to find where a broken document goes wrong.

You get JSON to copy or download. If the XML is not well formed, you get the line and column of the problem instead, with the offending line shown and a button to jump to it.

How it works

The document is read character by character, not picked apart with search patterns, so a > inside an attribute value is handled and a mismatched closing tag is an error. All five predefined entities and numeric references such as © are decoded. CDATA is kept exactly as written, while ordinary text has only its outer whitespace trimmed.

A child element becomes a key. When a name repeats, its key becomes a list. Attributes go under @attributes by default, or as individual @name keys. Namespace prefixes stay in the key unless you remove them.

A DOCTYPE is skipped, not obeyed. Custom entities defined in one are refused with an error, which also means an entity expansion attack such as the billion laughs has no effect here, and nothing external is ever fetched.

How to use XML to JSON

  1. Paste XML on the left, or choose Open file for a .xml, .rss or .atom file.
  2. If you see an error, select Show me in the input to go straight to that character.
  3. Under Options, pick an attribute style, and decide on namespace prefixes and number detection.
  4. Take the JSON with Copy or Download .json.

Attributes, a namespace, an entity and CDATA

Input
<book id="bk101" xmlns:dc="urn:dc"><dc:title>Pride &amp; Prejudice</dc:title><price currency="GBP">8.99</price><blurb><![CDATA[A <b>classic</b>]]></blurb></book>
Output
{
  "book": {
    "@id": "bk101",
    "@xmlns:dc": "urn:dc",
    "dc:title": "Pride & Prejudice",
    "price": {
      "@currency": "GBP",
      "#text": "8.99"
    },
    "blurb": "A <b>classic</b>"
  }
}

This run uses the As @name keys option. &amp; was decoded, the CDATA kept its tags as plain text, and price has both an attribute and text, so its text moved into #text.

When it helps

  • Reading an RSS or Atom feed in code that expects JSON.
  • Inspecting a SOAP response, with namespace prefixes removed to make it readable.
  • Migrating settings from an old XML configuration file to a JSON one.
  • Locating the exact fault in XML that another program rejected with a vague message.

Tips for a better result

  • Turn on Always use lists for child elements if code will consume the output. Otherwise an element that appears once is an object and one that appears twice is a list, which is the classic source of bugs in XML conversions.
  • XML text is all strings. Detect numbers and booleans converts values where that loses nothing, so 007 stays as text.
  • Removing namespace prefixes can merge elements from different namespaces that share a local name. They become one list, so leave prefixes on if that matters.

Good to know

  • Mixed content loses its order. In <p>Hello <b>big</b> world</p>, the text is gathered into #text and the b element sits beside it. A notice appears when this happens.
  • Comments and processing instructions are dropped.
  • It checks that a document is well formed, but it does not validate against a DTD or XSD schema.
  • Custom entities from a DOCTYPE are not expanded. HTML names such as &nbsp; are errors, as they are in any XML reader.

Questions people ask

Why is the same element sometimes an object and sometimes a list?

JSON needs a list to hold repeats, and a single occurrence looks like an ordinary value. Turn on Always use lists for child elements if you need the same shape every time.

My file has an ISO-8859-1 declaration. Will the accents survive?

Yes, when you open it as a file. The encoding named in the XML declaration is used to decode it, and a notice confirms this. Text you paste has already been decoded by your browser, so the declaration is ignored.

Why does it reject &nbsp; when my browser shows it fine?

HTML defines that name and XML does not. XML predefines only five names, so a strict reader must refuse it. Replace it with &#160;, which means the same character.

How are namespaces handled?

They are kept as written by default: dc:title stays as that key, and xmlns:dc appears among the attributes. Remove namespace prefixes shortens the key to title and drops the declarations. Prefixes are not resolved to their URIs.

What to use next

Last reviewed . Read our editorial policy.