Skip to main content
CalcChef

XML to JSON Converter

Convert XML to JSON and JSON back to XML: attributes go under @attributes, repeated elements become arrays, and an XML error shows its line and column. How it works

Use the XML to JSON Converter

Direction:
Format:

Up to 2,000,000 characters. Nothing is uploaded.

JSON Output
  • Skipped 1 comment: comments and processing instructions have no place in the JSON.

9 elements · 4 attributes

How the mapping works

  • Every value is text: 0123 stays "0123"; numbers and booleans are not guessed.
  • Attributes go under "@attributes"; text next to attributes or children goes under "#text"; a repeated element becomes an array.
  • Comments and processing instructions are skipped (and counted). A DOCTYPE or ENTITY declaration is refused.
  • Going back, names that are not valid XML names are rewritten and listed, a top-level array is wrapped in <root>, and characters XML 1.0 cannot hold are refused.

About the XML to JSON Converter

Each XML element becomes a JSON key, attributes go under "@attributes", a repeated element becomes an array and every value is a string, so <year>1965</year> gives "year": "1965". The converter also runs the other way, JSON to XML, and reports an XML error with its line and column.

XML and the JSON it gives

XMLJSON (written on one line)
<a>1</a>{"a":"1"}
<a/>{"a":""}
<a id="1">x</a>{"a":{"@attributes":{"id":"1"},"#text":"x"}}
<a><b>1</b><b>2</b></a>{"a":{"b":["1","2"]}}
<a>0123</a>{"a":"0123"}
<a><![CDATA[<x>]]></a>{"a":"<x>"}
<p>Hello <b>you</b> there</p>{"p":{"b":"you","#text":["Hello","there"]}}

JSON and the XML it gives

JSONXML (no declaration, on one line)
{"a":{"@attributes":{"id":"1"},"#text":"x"}}<a id="1">x</a>
{"a":null}<a/>
[1,2]<root><item>1</item><item>2</item></root>
{"a":1,"b":2}<root><a>1</a><b>2</b></root>
{"1st":"x"}<_1st>x</_1st>
{"ns:item":"v"}<ns_item>v</ns_item>

XML the converter refuses, and its message

XMLMessage
<a><b></a>Line 1, column 7: The closing tag </a> does not match the open element <b>.
<a>&copy;</a>Line 1, column 4: The entity "&copy;" is not defined. Only &amp; &lt; &gt; &quot; &apos; and numeric references such as &#65; are supported (a DOCTYPE cannot define more).
<!DOCTYPE a><a/>Line 1, column 1: This document has a DOCTYPE / ENTITY declaration. Those are not supported (they can define entities that expand into huge text), so the document is refused. Remove the <!DOCTYPE ...> part.

Frequently asked questions

Why are numbers strings in the JSON?
XML has no number type: <year>1965</year> and <a>0123</a> are both text. The converter keeps "1965" and "0123" as strings rather than guessing, so an ID or ZIP code never loses a leading zero.
How are XML attributes converted to JSON?
They go into an "@attributes" object on the element, and the element’s text goes under "#text": <a id="1">x</a> gives {"a":{"@attributes":{"id":"1"},"#text":"x"}}. An element with attributes and nothing inside, such as <a id="1"/>, gives only the "@attributes" object, with no "#text" key. Attributes keep the order they had in the tag.
Why is my XML with a DOCTYPE refused?
A DOCTYPE can define entities that expand into huge text, so any <!DOCTYPE or <!ENTITY is refused and no entity is ever expanded. Only the five predefined entities, &amp; &lt; &gt; &quot; and &apos;, and numeric references such as &#65; are read; &copy; is an error.
What happens to text mixed with child elements?
<p>Hello <b>you</b> there</p> gives "#text": ["Hello","there"] next to "b": "you", with a note. The text pieces keep their order, but where they sat between the child elements is lost, so the JSON cannot rebuild that paragraph exactly.
Can it convert JSON back to XML?
Yes, with JSON → XML. Keys that are not valid XML names are rewritten and listed ("1st" becomes _1st and "ns:item" becomes ns_item), a top-level array or several top-level keys are wrapped in <root>, and control characters XML 1.0 cannot carry are refused with their path.
What happens to comments and namespaces?
Comments and processing instructions are skipped and counted in a note. Namespace prefixes stay in the key names, such as "ns:item" and "xmlns:ns", but are not resolved. Converting that JSON back to XML replaces each colon with an underscore, so <ns:item xmlns:ns="u">v</ns:item> comes back as <ns_item xmlns_ns="u">v</ns_item>: namespaced XML does not survive a round trip.

Related Tools

View all tools