How to use XML to JSON
- 1Paste XML into the box, or open or drop an .xml file.
- 2Choose how attributes are written: prefixed keys (default @), grouped in an @attributes object, or ignored.
- 3Choose the array rule (only repeated elements, or always) and the key used for text next to child elements.
- 4Optionally convert numbers and true/false, remove namespace prefixes and trim whitespace.
- 5Copy the JSON or download it. If the XML is invalid, fix the reported line and the JSON appears.
How XML maps to JSON
XML has features JSON does not have, so every converter has to make choices. Here they are yours:
- Attributes: <price currency="USD"> becomes {"@currency": "USD"}. Change the prefix to _ or -, leave it empty, group attributes under @attributes, or drop them.
- Text: an element with only text becomes a plain value. When an element has attributes or children as well as text, the text goes under #text, or any key you choose.
- Arrays: by default only elements that repeat become arrays, which gives the most natural JSON. Choose Always if your code needs a consistent shape whether there is one <item> or many.
- Types: numbers and true/false can be converted, and values with leading zeros stay strings.
- Namespaces: soap:Body can become Body, and xmlns declarations are dropped.
CDATA sections are kept as text, character references like 가 are decoded, and comments and processing instructions are skipped.
Clear errors for broken XML
Instead of a generic parse error, you get the line and column and a plain explanation: a closing tag that does not match the tag opened on line 12, an attribute without quotes, a stray & that should be &, an HTML entity like that XML does not know, a tag that is never closed, or a second root element. The faulty line is shown with a marker, and Show in editor jumps to the spot. The checks are the same in every browser because the parser is built into the page instead of relying on the browser's own XML parser.
Need to tidy the result? Paste it into the JSON Formatter. To turn it into a table, use JSON to CSV.
One result versus many: a tested API response
Many government and enterprise APIs return XML shaped like response > body > items > item. We converted the same response with one item and with two, using the default settings:
- With one item,
items.itemcame out as a single object:{"stationName": "종로구", "pm10Value": "32", ...}. - With two items,
items.itemcame out as an array of two objects.
Code written against the two-item shape breaks on the first day the query returns one result. If your code loops over the items, set Arrays to Always so the shape never changes. Also note that values stay strings by default: "32", and the result code "00". Turn on number conversion if you want 32, and leading zeros such as 00 still stay strings.
Placeholders and mixed values
APIs often put a placeholder such as - in a field when a measurement is missing. In our sample, pm25Value was "-" in one item and "15" in the other. With number conversion on, the same field becomes a string in one record and a number in the next, which trips up code and charts that expect one type. Decide in your code what a placeholder means (usually null) before you add numbers up.
XML names are case-sensitive, so <resultCode> closed with </resultcode> is an error, reported here as a closing tag that does not match its opening tag, with the line and column. Hand-edited XML fails this way more often than any other.
Frequently asked questions
How are XML attributes converted?
By default as keys starting with @, for example {"@id": "bk101"}. You can change the prefix, group them under @attributes, or ignore attributes completely.
Why is an element sometimes an object and sometimes an array?
With the default rule, an element becomes an array only when it appears more than once in the same parent. Choose Always under Arrays to make every element an array.
My XML will not convert. How do I find the error?
The error box shows the line, column and reason, and the faulty line with a marker. Click Show in editor to jump to that position in the input.
Are DTDs or external entities loaded?
No. DOCTYPE declarations are skipped and external entities are never fetched, so converting untrusted XML is safe.
Can it handle large XML files?
Yes. Large files are shown as a file card instead of in the text box and are parsed in a background thread. The output box shows the beginning, while Copy and Download include everything.
Why is item an object in one response and an array in another?
With the default rule, an element becomes an array only when it repeats. One result gives an object; two or more give an array. Set Arrays to Always to get an array every time.
Why are numbers in quotes?
XML has no number type, so values are kept as strings by default. Turn on number and true/false conversion to get real numbers; values with leading zeros, such as a result code of 00, stay strings.
Why does a correct-looking closing tag cause an error?
XML tag names are case-sensitive, so resultCode and resultcode are different names. Make the closing tag match the opening one exactly.