Why convert XML to JSON?
- Modern tooling. JSON is native to JavaScript and one line to parse in Python, Go, Ruby, and every web stack — no XML DOM to walk.
- Nothing dropped. Attributes land in
@attributesand mixed text in#text, so the metadata XML carries survives the trip. - Typed, not stringy. Numbers and booleans come out as real JSON types, so
<price>9.99</price>is a number you can sum, not a string you have to parse.
How the XML → JSON mapping works
Every converter has to decide how XML's attributes, text, and repeated elements land in JSON. Here's exactly what this one does:
- Attributes →
@attributes. Each element's attributes are grouped into an@attributesobject on that element. - Element text →
#text(or a scalar). If an element has attributes and text, the text goes under#text. A leaf element with only text becomes that value directly. - Repeated tags → arrays. Sibling elements sharing a tag become a JSON array; a single occurrence stays a single object.
- Values are typed. Numeric text becomes a number,
true/falsebecome booleans, an empty element becomesnull; everything else stays a string. - Root wrapper. The whole result is wrapped in one object keyed by the root element's tag.
So this XML:
<book id="1"><title>Dune</title><inStock>true</inStock></book>
becomes this JSON:
{ "book": { "@attributes": { "id": "1" }, "title": "Dune", "inStock": true } }
How it works
- Use the converter. Go to the Formatly XML to JSON converter page. No registration is required.
- Upload your XML file. Drag and drop your XML document into the uploader, or browse from your device.
- Convert to JSON. Select JSON as the target format and click the Convert button.
- Download the JSON file. Click the Download button to save the converted JSON document.
Good for
- Consuming a legacy XML feed in a modern app.
- Converting SOAP- or RSS-style XML to JSON.
- Simplifying XML data for front-end use.
XML to JSON in code — PHP, JavaScript, Python
Converting in a script instead? The usual one-liners: in PHP, json_encode(simplexml_load_string($xml)) (attributes land under @attributes, like here); in Node.js, the xml2js or fast-xml-parser npm packages; in the browser, DOMParser plus a small walk of the tree — that's exactly what the paste tool above does; in Python, xmltodict.parse(). For a one-off feed or API response, pasting it above is faster than wiring up a parser.
FAQ
Can I convert XML to JSON without uploading a file? Yes. Paste your XML into the instant converter on this page and the JSON appears as you type. The paste tool runs entirely in your browser, so the XML never leaves your machine. Use the file uploader for large files or batches.
How are XML attributes handled? Attributes are collected into an @attributes object on the element they belong to, so nothing is lost. If an element also has text, that text is placed under a #text key alongside @attributes. A plain element with only text becomes that value directly.
Are numbers and booleans converted to real JSON types? Yes. Text that looks numeric becomes a JSON number, true and false become booleans, and an empty element becomes null. Everything else stays a string. One caveat: a value like 007 is read as the number 7, so if leading zeros matter, keep that field as text in the source.
What happens to repeated tags? Repeated sibling elements with the same tag become a JSON array. Three <item> elements under a parent turn into an item array; a single occurrence stays a single object. The whole document is wrapped in one object keyed by the root element's tag.
Can I convert JSON back to XML? Yes. Use the JSON to XML converter for the reverse direction. Note that a round-trip won't always be byte-identical, because JSON has no separate notion of attributes versus child elements.
Is my data secure? Files are processed over an encrypted connection and automatically deleted after 1 hour. No signup is required and files are never used to train any AI model.
Related
- JSON → XML → go the other way
- CSV → JSON → convert a CSV instead
- YAML → JSON → convert YAML instead
- JSON → YAML → turn the result into config
- How to convert JSON to CSV → get JSON into a spreadsheet
- All supported formats →