XML to JSON
Convert XML to JSON
About XML to JSON
Converts an XML document into JSON using the compact convention of the xml-js library: attributes go under _attributes, text content under _text, and repeated elements become arrays. Everything runs in your browser.
When you would use it
- Reading a SOAP response, an RSS feed or a configuration file as JSON in a script or a browser tool.
- Exploring the structure of an unfamiliar XML document.
- Preparing XML data for a JSON-only API or a NoSQL database.
How to use it
- Paste the XML into the left field. Invalid XML is flagged with an error.
- Read the JSON on the right.
- Copy the result.
Things worth knowing
- Attributes are grouped in an _attributes object and element text in _text, so <a id="1">hi</a> becomes {"a":{"_attributes":{"id":"1"},"_text":"hi"}}.
- An element that appears once becomes an object, the same element appearing twice becomes an array. Code that reads the result must handle both shapes, or normalise them.
- All values are strings. XML has no types, so 42 and true stay "42" and "true".
- The XML declaration, comments, CDATA and processing instructions are kept under keys such as _declaration, _comment and _cdata. Namespaces are kept as part of the element name (ns:item).
Frequently asked questions
- Why is the same element sometimes an object and sometimes an array?
- The converter cannot know which elements are meant to repeat. One occurrence gives an object, two or more give an array. Normalise with something like [].concat(value) in your code.
- Why do my numbers appear as strings?
- XML carries no type information, so every value is text. Convert to numbers or booleans in the consuming program.