XML ⇄ JSON
Convert between XML and JSON, from text or a file.
About XML ⇄ JSON
Convert XML to JSON or JSON to XML in the browser. Paste text or drop a file, the direction is detected from the input, and the result appears as you type.
How to use
- Paste XML or JSON into the input, or load a
.xmlor.jsonfile (up to 5 MB) with the file picker or by dropping it on the page. - The direction is detected automatically: input that starts with
{or[is treated as JSON, anything else as XML. Force a direction with the Auto / XML → JSON / JSON → XML switch if the guess is wrong. - The output updates as you type. Use Copy, or Download to save it as
.jsonor.xmlunder the loaded file's name.
How the mapping works
- XML is parsed with the browser's
DOMParser, so malformed XML is reported with the parser's line and column rather than guessed at. - Each element becomes a key; repeated sibling elements with the same name become an array.
- Element attributes are collected under an
@attributeskey. Text that sits beside attributes or child elements is kept under#text. - An element with no attributes and no child elements becomes its trimmed text.
- JSON → XML uses a single top-level key as the root element; JSON with several keys, an array at the root or an array under the only key is wrapped in
<root>. An@attributesobject becomes attributes, a#textvalue becomes the element text,nullbecomes an empty element and an array repeats the element once per item. A key that is not a valid XML name is reported as an error. Output starts with an XML declaration and is indented with two spaces. - Namespace prefixes are kept as part of the key name (
ns:item). Comments are ignored.
Example
<note id="1">
<to>Ada</to>
<body>Hello</body>
</note>
→
{
"note": {
"@attributes": { "id": "1" },
"to": "Ada",
"body": "Hello"
}
}
Privacy
Parsing and conversion run in the page. A dropped file is read locally, no request is made, and nothing is stored.
Frequently asked questions
- Is there a standard way to convert XML to JSON?
- No. XML has attributes, ordering, namespaces and mixed content that JSON does not, so every converter picks its own mapping. XUtil uses the common convention of an @attributes object for attributes and arrays for repeated elements.
- Why do XML attributes end up under a key called @attributes?
- JSON has no attribute concept, so attributes have to live somewhere inside the element object. Putting them under @attributes keeps them separate from child elements that might share a name.
- What becomes the root element when my JSON has several keys?
- An XML document must have exactly one root element. A single top-level key is used as that root; otherwise the converter wraps the whole document in a <root> element, and a root array becomes <item> elements inside it.
- Does the converter upload my XML file?
- No. The file is read with the browser File API and converted in the page, so its contents never leave your machine.