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
Up to 2,000,000 characters. Nothing is uploaded.
- 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:
0123stays"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
| XML | JSON (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
| JSON | XML (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
| XML | Message |
|---|---|
| <a><b></a> | Line 1, column 7: The closing tag </a> does not match the open element <b>. |
| <a>©</a> | Line 1, column 4: The entity "©" is not defined. Only & < > " ' and numeric references such as A 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, & < > " and ', and numeric references such as A are read; © 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 toolsCSV to JSON
Convert CSV to JSON: the first row becomes the keys, quoted commas and line breaks are kept, and numbers and booleans are detected only if you ask.
JSON to CSV
Turn JSON into CSV: an array of objects, one object or a list of values. Flatten nested objects, expand arrays into rows, then copy or download.
YAML to JSON Converter
Convert YAML to JSON with anchors and merge keys resolved, YAML 1.2 or 1.1 typing for yes/no, and a warning for numbers JSON cannot hold.
JSON to YAML
Convert JSON to YAML in block or flow style. Text such as yes or 017 is quoted so it stays a string, and a JSON error shows its line and column.
SQL to JSON
Turn SQL INSERT statements, or a result table pasted from psql, MySQL or Markdown, into JSON. Quoted values stay strings; numbers, true and NULL get types.
JSON to SQL
Generate SQL INSERT statements, and CREATE TABLE if you want it, from JSON for PostgreSQL, MySQL, SQLite or SQL Server, with types inferred from every row.