Skip to main content

XML ⇄ JSON Converter

Convert XML to JSON and back in your browser. Attributes become @name, text nodes become #text, and repeated sibling elements become arrays.

Convert XML into JSON and JSON back into XML, using a consistent convention for the two things JSON has no native equivalent of: attributes become keys prefixed with an at sign, and element text becomes a #text key. Repeated sibling elements collapse into an array.

XML ⇄ JSON Converter
Indent
Paste some XML...

💡 XML attributes become @name and text nodes become #text. Repeated sibling elements become an array.

// XML ⇄ JSON Converter: Features

The mapping, and why it needs one

XML and JSON do not describe the same thing. An XML element can carry attributes as well as children and text, while a JSON object has only keys and values. The convention used here resolves that: an attribute named lang becomes the key @lang, the text content of an element that also has attributes or children becomes #text, and an element with nothing but text becomes a plain value. Knowing the convention is what makes the output predictable rather than surprising.

Repeated elements become arrays

Two sibling elements with the same name become an array in the JSON output, and a single one becomes a plain object. That is the natural mapping, and it introduces the single biggest gotcha when consuming converted XML: a list that happens to contain one item looks structurally different from a list containing two. Code that reads the result should handle both, which is why many XML-derived APIs are awkward to work with regardless of the converter used.

What the parser handles

Elements, attributes, text content, CDATA sections, comments, processing instructions and DOCTYPE declarations are all recognised. Comments, processing instructions and DOCTYPE are skipped rather than represented, because they carry no data in the sense JSON understands. Character entities including the five predefined ones and numeric references in decimal and hexadecimal are decoded, and re-encoded when converting back.

Where you still meet XML

It remains the language of SOAP services, of RSS and Atom feeds, of SAML assertions in enterprise single sign-on, of SVG, of Android layouts, of Maven and many other build systems, and of a great many government and financial data formats. Converting to JSON is usually a step towards working with that data in a modern stack, where JSON is what the rest of the code expects.

SOAP responses and SAML assertions stay local

Parsing and serialisation run as JavaScript in your browser. SOAP responses containing customer records, SAML assertions carrying identity claims and internal configuration files can all be pasted without being transmitted, stored or logged. The SAMPLE button loads a small worked example so you can see the convention applied before using your own data.

// XML ⇄ JSON Converter: FAQ

How are XML attributes represented in JSON?

As keys prefixed with an at sign. An element written with category equals programming produces the key @category in the resulting object. The prefix keeps attributes distinct from child elements, which could otherwise collide with an attribute of the same name.

What is the #text key?

It holds the text content of an element that also carries attributes or child elements, because in that case the text cannot simply be the value of the element. An element containing only text and nothing else becomes a plain string, number or boolean directly, with no #text wrapper.

Why is my single-item list an object rather than an array?

Because the converter can only see what is in the document, and one element is indistinguishable from a list of one. This is inherent to XML rather than a flaw in the conversion: the schema knows the element repeats, the instance document does not. Consuming code should accept either shape, or normalise by wrapping non-arrays before processing.

Are values converted to numbers and booleans?

Yes, where they clearly look like one. A text value of true or false becomes a boolean, null becomes null, and a purely numeric value becomes a number. That makes the output pleasant to work with, but be aware that an identifier consisting only of digits will lose any leading zeros, so quote or post-process those if they matter.

What happens to comments and the DOCTYPE?

They are skipped. JSON has no representation for them and they carry no data, so including them would only complicate the output. The same applies to processing instructions, including the XML declaration at the top of a document.

Does it handle namespaces?

Prefixed names are kept as they are, so an element written as soap:Body becomes the key soap:Body rather than being resolved against a namespace declaration. For most practical conversion work that is what you want, because the prefix is how the data is usually referred to. Full namespace resolution would need schema awareness the tool does not have.

Can it convert JSON back into XML?

Yes, following the same convention in reverse: keys beginning with an at sign become attributes, a #text key becomes element text, and an array becomes repeated sibling elements. Special characters are re-encoded as entities so the output is well formed.

Is CDATA supported?

Yes. The contents of a CDATA section are taken as literal text without entity decoding, which is the point of the construct. This matters for feeds and documents that embed HTML inside an XML element.

Is it safe to paste a SAML assertion or a SOAP response?

Yes. Everything is parsed in your browser and the document is never transmitted, stored or logged. As with any credential-bearing material, treat something you have displayed on a shared screen as exposed.

Is the XML or JSON I paste sent to a server?

No. The conversion runs entirely in the page, and closing the tab discards what you pasted.

// How to Use XML ⇄ JSON Converter

  1. XML to JSON, or JSON to XML

    Select XML to JSON or JSON to XML. Press SAMPLE to load a small worked example in the current direction, which shows how attributes, text nodes and repeated elements are mapped.

  2. Paste the XML or the JSON

    Put the XML or JSON into the input box. Conversion happens as you type, and a malformed document is reported with a message naming the problem, such as an unclosed tag or mismatched element names.

  3. Set the indentation and copy the markup

    Choose the indentation for the output and copy it with one click. The key count underneath is a quick check that the structure came through complete.

Category Data Formats