XML to JSON Converter
Convert an XML file into JSON. Attributes are prefixed (@ by default), sequences of similar elements become arrays and text nodes become #text properties. Add a _type attribute to infer JSON types.
prefix + name. Leave empty for no prefix.Result
XML to JSON conversion rules
XML and JSON do not have the same data model: XML has attributes, ordered mixed content, namespaces and comments, while JSON only knows objects, arrays and a handful of scalar types. The converter therefore applies a set of deterministic rules, similar to the ones used by most XML/JSON libraries (Jackson, xml2js, Newtonsoft…):
- The document becomes an object with a single property named after the root element:
{ "root": … }. - An element that contains only text becomes a string (its text, trimmed). An empty element (
<empty/>or<empty></empty>) becomesnull. - An element with child elements becomes an object; each child becomes a property named after the child's element name. Element names keep their namespace prefix (
foo:cd). - Attributes become properties prefixed with the chosen prefix (
@by default):<cd id="1">gives"@id": "1". When an element has attributes and text, the text goes into the#textproperty (name configurable). - A sequence of two or more sibling elements with the same name becomes a JSON array. A single child stays a plain value, unless "Always wrap child elements in arrays" is checked, which is useful when your code expects a stable shape whatever the number of items.
- Namespace declarations (
xmlns,xmlns:foo) are kept as attributes by default; uncheck the option to drop them. - Comments are dropped unless "Include comments" is checked, in which case they appear as
#commentproperties (an array when there are several). - Text is kept as a string unless "Convert numbers and booleans automatically" is checked, or a
_typeattribute is used (see below). Property order follows the document order.
Example
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:foo="http://www.foo.org/">
<foo:cd id="1">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price currency="USD">10.90</price>
<notes/>
</foo:cd>
<foo:cd id="2">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price currency="GBP">9.90</price>
</foo:cd>
</catalog>
Output JSON:
{
"catalog": {
"@xmlns:foo": "http://www.foo.org/",
"foo:cd": [
{
"@id": "1",
"title": "Empire Burlesque",
"artist": "Bob Dylan",
"price": { "@currency": "USD", "#text": "10.90" },
"notes": null
},
{
"@id": "2",
"title": "Hide your heart",
"artist": "Bonnie Tyler",
"price": { "@currency": "GBP", "#text": "9.90" }
}
]
}
}
The _type attribute: choosing the JSON type of a value
XML has no notion of data types, so every value is a string by default. To get real JSON numbers and booleans you can either enable "Convert numbers and booleans automatically" (every value that looks like a number or a boolean is converted) or annotate individual elements with a _type attribute. The attribute itself is removed from the output.
| XML | JSON |
|---|---|
<active _type="boolean">true</active> | "active": true |
<age _type="integer">42</age> | "age": 42 |
<price _type="float">10.90</price> | "price": 10.9 |
<total _type="number">1e3</total> | "total": 1000 |
<zip _type="string">01234</zip> | "zip": "01234" (never converted, even with automatic conversion on) |
Accepted values: boolean, integer, float, number and string. A value that cannot be converted to the requested type is kept as a string.