XML to JSON
Convert XML documents to JSON.
About the XML to JSON
Turns an XML document into JSON you can actually work with. Attributes are preserved rather than discarded, so id="1" becomes the key @_id, and repeated child elements collapse into a JSON array. The document is validated before conversion, so malformed XML gives you the specific error instead of a half-built result. Everything runs in your browser, so SOAP responses and config files stay on your machine.
How to use the XML to JSON
- Paste your XML into the XML panel, or press Sample to load an example.
- Pick 2 spaces or 4 spaces from the Indent selector.
- Press Convert to produce the JSON.
- Check any element that can repeat, because one child yields a value and two yield an array.
- Copy the result, or Download it as converted.json.
Frequently asked questions
- What happens to XML attributes?
- They become keys prefixed with @_, sitting alongside the element's children, so <user id="1"><name>Ada</name></user> converts to {"user":{"name":"Ada","@_id":"1"}}. When an element carries both text and an attribute the text moves to #text, so <price currency="USD">9.99</price> gives {"#text":9.99,"@_currency":"USD"}.
- Why is a repeated element an array but a single one is not?
- XML has no syntax for a list, so the shape is inferred from what is present. Two roles convert to "roles":{"role":["admin","engineer"]}, while one converts to "roles":{"role":"admin"}. Any list that can legitimately hold a single item has to be handled in both shapes by the code reading the JSON.
- Are numbers and booleans converted?
- Element text that looks numeric or boolean is converted, so <count>42</count> gives 42 and <a>true</a> gives true. That also costs leading zeros, since <zip>07030</zip> becomes 7030. Attribute values are the exception and always stay strings, which is why @_id is "1" rather than 1.
- What does the converter drop or reshape?
- Comments are discarded, CDATA is unwrapped to its text, and empty or self-closing elements become an empty string. The XML declaration becomes a "?xml" key, and namespace prefixes are kept in the key name, so ns:a stays "ns:a". Mixed content loses position: <p>hello <b>world</b> end</p> gives {"b":"world","#text":"helloend"}.
- What if my XML is invalid?
- It is validated first, so you get the specific problem rather than JSON built from a broken parse. A missing close tag reports "Expected closing tag 'a' (opened in line 1, col 7) instead of closing tag 'root'", and two top-level elements report "Multiple possible root nodes found".