JSON1 / XML to JSON
XML to JSON
Convert XML to formatted JSON. Attributes become @name keys, repeated elements collapse into arrays, and mixed text lands under #text.
Processed locally·Your data never leaves your device
InputXML
ReadyOutputJSON
Converted output appears here as you type.
Good to know
- XML has no type system, so values are guessed: "10001" becomes the number 10001 and an empty element becomes null. Round-tripping JSON through XML will not always return the original types.
- Namespace declarations are markup rather than content, so xmlns and xmlns:* are not carried through as keys.
About this tool
Usually this is about getting a legacy or third-party payload into a shape modern code can work with. The mapping has to invent conventions, because XML distinguishes several things JSON does not — and every XML-to-JSON tool invents them slightly differently, which is worth knowing if you are comparing output.
The conventions here are the common ones:
@idAn attribute becomes a key with an @ prefix, so it cannot collide with a child element of the same name.#textText sitting alongside child elements lands here, rather than being dropped as it would be by a naive walk.[...]Repeated sibling elements collapse into an array. One occurrence stays a scalar, because nothing in the document says it was a list.nullAn empty element becomes null, however it is written — <a/> and <a></a> are the same element, so they cannot give different JSON. So does one marked xsi:nil.
The one thing no convention can fix is types. XML has none — every value is text — so they have to be guessed, and the guessing is where data gets quietly changed.
- Why did my postcode 02134 become the number 2134?
- Because it looks numeric, and a number is the useful guess for the overwhelming majority of values. Identifiers, postcodes, phone numbers, and version strings are the exceptions, and they are exactly the fields where losing a leading zero matters. Check them before relying on the output.
- Can I round-trip JSON through XML and back?
- Not reliably, and the type guessing is why. A string
"10001"goes out as<zipCode>10001</zipCode>, which has no way to say it was a string, so it comes back as a number. Structure survives the trip; types do not. - Where did my attributes go?
- Nowhere —
id="7"becomes the key@id. The prefix is what keeps it separate from a child element also calledid, which is legal XML and would otherwise overwrite one with the other. - What about namespaces?
- Prefixes are kept as part of the key, so
<ns:name>becomesns:name. Nothing is resolved against the declaration, because JSON has no way to express a namespace — the prefix is preserved as a name rather than interpreted.
Related tools
- JSON to XMLConvert JSON to indented XML. Arrays repeat the parent tag, nulls become xsi:nil, and keys that are not legal element names are sanitised.
- JSON to TOMLConvert JSON to TOML. Nested objects become tables, arrays of objects become table arrays, and everything else inlines.
- JSON to GoGenerate Go structs with JSON tags from a JSON sample. Optional fields become pointers tagged omitempty, so encoding/json can tell absent from the zero value.