XML to JSON Converter

Convert XML to clean JSON in your browser, with control over attributes, arrays and value types.

JSON to XML

About XML to JSON Converter

What XML to JSON conversion actually involves
XML and JSON are not two spellings of the same thing, which is why no single "correct" conversion exists. XML has attributes, a distinction between an attribute and a child element, and ordered mixed content where text and elements interleave. JSON has none of those, but it does have real arrays, real numbers and real booleans, which XML lacks — every XML value is text until something interprets it. A converter therefore has to make choices, and the useful thing a tool can do is make those choices explicit rather than hide them.

Why convert XML to JSON?
Almost always because something upstream speaks XML and something downstream speaks JSON. A SOAP endpoint, an RSS or Atom feed, a bank or carrier integration, an Android or Spring config file, a sitemap, an Office document part — these still arrive as XML, while the JavaScript frontend, the REST API or the JSON column in your database expects JSON. Converting also just makes the data easier to read: a deeply nested XML document is far quicker to scan as JSON, and you can drop the result straight into a test fixture or a fetch() mock.

The mapping convention this tool uses
Attributes become keys prefixed with @, and an element's own text becomes #text when the element also carries attributes or children. This is the convention used by fast-xml-parser, xml2js and most cloud SDKs, so the output is what other tooling expects — and, importantly, it is lossless, so you can convert back again without having thrown anything away:

<book id="1" lang="en">
  <title>XML Guide</title>
  <price cur="USD">44.95</price>
</book>
{
  "book": {
    "@id": "1",
    "@lang": "en",
    "title": "XML Guide",
    "price": {
      "@cur": "USD",
      "#text": "44.95"
    }
  }
}

An element with nothing but text and no attributes collapses to that text — <title>XML Guide</title> becomes just "title": "XML Guide" rather than a pointless wrapper object. Empty elements become an empty string. Entity references such as &amp; and &#39; are decoded into the real characters, and CDATA content is carried through literally.

How to use this tool:
1. Paste your XML into the left pane, or click "Load sample" to see a worked example.
2. Optionally tick "Cast numbers & booleans" and "Always use arrays for child elements" — both are explained below.
3. Pick your indentation (2 spaces, 4 spaces, Tab, or Minified for a single line).
4. Click "Convert". If the XML is malformed, the right pane shows the message, the offending line and a caret under the exact column that broke it.
5. Use "Copy" or "Download" in the toolbar to take the JSON away. Changing an option after converting re-runs the conversion immediately.

The two traps worth knowing about
One item is not an array. When an element appears twice it naturally becomes a JSON array; when it appears once, it becomes a single object. That means code written against a response containing three <item> elements will break the day a response contains one, because data.item.map()is suddenly not a function. This is the most common XML-to-JSON bug in production, and it is why "Always use arrays for child elements" exists — turn it on and every child element is an array, however many times it occurs, so your consuming code has one shape to handle.

Casting numbers can corrupt your data. Turning "44.95" into 44.95 is usually what you want; turning an order reference like "007" into 7, a version string like "1.2.3" into something unparseable, or a 20-digit account number into a rounded float is not. Casting is therefore off by default, and when you switch it on this tool only casts a value if converting it to a number and back reproduces the original text exactly — so long IDs, leading zeros, trailing zeros and version strings are all left as strings.

Privacy & Security Guarantee
The XML parser and the JSON writer both run inside your browser as client-side JavaScript. There is no upload and no server-side processing, which matters because the XML people need to convert is so often a SOAP envelope with credentials in its header, a production config file, or a customer data export. None of it leaves your machine. Need the other direction? Use the JSON to XML Converter. Just want to tidy up XML without converting it? The XML Formatter & Validator beautifies, minifies and validates in place.

Frequently Asked Questions

Is my XML data secure?

Yes. Parsing the XML and generating the JSON both happen entirely inside your browser using client-side JavaScript. Nothing is uploaded, logged or sent to a server, so it is safe to convert SOAP envelopes, production configuration and customer data exports.

Why is my single element an object instead of an array?

Because XML has no way to say 'this is a list'. An element that appears twice becomes a JSON array; the same element appearing once becomes a single object, since nothing in the document marks it as repeatable. This is the classic bug where code that works on three items crashes on one. Tick 'Always use arrays for child elements' and every child element is emitted as an array regardless of how many times it appears, giving your consuming code a single predictable shape.

Why did my ID or version number change?

That is number casting, and it is off by default for exactly this reason. When you enable it, this tool only casts a value if converting it to a number and back produces the original text character for character. So 44.95 and true are cast, while '007', '1.2.3', '+1', '1.50', 'Infinity' and long identifiers such as '12345678901234567890' all stay strings — a naive converter would turn those into 7, NaN and a rounded float respectively.

What happens to attributes, comments and the XML declaration?

Attributes are preserved as keys prefixed with @, so id="1" becomes "@id": "1". An element's own text becomes "#text" when that element also has attributes or children. Comments, the <?xml ... ?> declaration, the DOCTYPE and processing instructions are all dropped, because JSON has no equivalent for any of them. CDATA content is kept as literal text, and entity references like &amp; are decoded into the real characters.

Is the conversion lossless — can I convert back to XML?

For structure and data, yes: because attributes and text are preserved rather than discarded, you can take the JSON to our JSON to XML converter and get an equivalent document back. Two things genuinely cannot survive the trip. Comments and the DOCTYPE are dropped, as above. And mixed content — text interleaved with child elements, like <p>Hello <b>world</b> now</p> — loses its ordering, because a JSON object cannot express 'text, then an element, then more text'; the text runs are collected into #text instead.

Can it handle large XML files?

Yes. Since the work happens on your own machine there is no upload limit and no server timeout — the only ceiling is your browser's available memory. Documents in the low tens of megabytes convert fine, though a very large file may make the tab pause briefly while the result is rendered.