File & Data
How to Convert XML to JSON in Your Browser
XML to JSON exists for the moment your data needs to move from an XML-based system into JavaScript, a REST API, or any other tool that speaks JSON natively. This guide explains the mapping rules well enough that you can predict what your document will look like before you convert it, and where a straight XML-to-JSON translation stops being the right fit.
Ready to try the tool this guide describes?
What happens during the conversion
The converter walks the XML tree and applies the same deterministic rules to every element, so the same document always produces the same JSON shape. Attributes on an element are collected into an @attributes object; a single child element becomes a normal key on its parent; and any element name that repeats among its siblings becomes a JSON array instead, with entries kept in the order they appeared in the source document.
Text content is handled differently depending on what else is on the element. An element that is only text — no attributes, no child elements — becomes a plain JSON string. An element that also carries attributes or children keeps its text under a #text key alongside those attributes, rather than losing it.
What to watch for
All text stays as a JSON string. Numbers, booleans, and empty elements are not inferred or cast — "0", "false", and an element with no content all come through as strings, so code consuming the JSON needs to do its own type conversion for anything beyond plain text.
Namespace prefixes are preserved as part of the element’s key using the browser’s DOM nodeName, so a tag like <ns:item> becomes a JSON key literally named "ns:item" rather than being resolved to a plain "item" or stripped of its prefix. xmlns declarations themselves show up as ordinary attributes under @attributes.
Comments and processing instructions are dropped entirely — only element, attribute, and text content make it into the JSON — so anything encoded in a comment will not survive the round trip.
When this conversion is the right move — and when it isn’t
It’s the right tool when you’re modernizing an XML feed or config file into something a JavaScript app or JSON-based API can consume directly, or when you just need a quick, readable look at an XML document’s structure. The workflow itself is simple: upload a file or paste XML, check the preview, and convert.
It’s a weaker fit if you need the output to match a specific JSON schema your application already expects — the mapping is generic and structural, not tailored to any particular shape — or if the document relies on comments, processing instructions, or precise type information that this text-only, structural mapping does not attempt to preserve.
Limits and privacy
XML input is capped at 25 MB whether you upload a file or paste text, nesting deeper than 50 levels is refused outright, and generated JSON output is capped at 50 MB. Documents with a DOCTYPE or DTD declaration are rejected before parsing, and only UTF-8-encoded XML is accepted — these are hard rejections rather than best-effort conversions, since DOCTYPE/DTD constructs are a known vector for entity-expansion attacks.
Parsing and conversion both run in your browser using DOMParser and a shared XML security module; there is no Looty Tools conversion endpoint that receives your XML contents.
How to Convert XML to JSON FAQ
- When does converting to JSON make more sense than just working with the XML directly?
- Mainly when the next step in your workflow — a JavaScript app, a JSON-based API, a NoSQL document store — doesn’t speak XML natively, or when you want a faster visual read of a deeply nested document than raw angle brackets give you.
- How can I predict whether a given element will become a plain string or an object with #text?
- Check whether that element has attributes or child elements of its own. If it’s pure text with neither, it becomes a plain string; if it has attributes or children alongside its text, the text moves into a #text key so it isn’t lost next to the rest of the object.
- Does the JSON preserve the order elements appeared in the original document?
- Yes for repeated elements — when siblings with the same name become an array, they keep the order they had in the source XML. A single, non-repeated element simply becomes a normal key rather than a one-item array.
- Why would my file get rejected outright instead of converted with a warning?
- The security checks — DOCTYPE/DTD rejection, UTF-8-only encoding, and a 50-level nesting cap — are hard stops rather than best-effort fixes, because constructs like external DTDs are a known way to smuggle in entity-expansion attacks, so the converter refuses the whole document rather than trying to sanitize it.
- Will the JSON be ready to use as-is, or should I expect cleanup afterward?
- Expect some cleanup if your code needs real types. Every value converts to a string, so numeric IDs, booleans, and dates all need explicit parsing on the receiving end — the mapping preserves structure faithfully but does not infer data types.
Related guides
- XML to JSON Without UploadingSee how browser-based XML to JSON keeps documents on your device, including DOCTYPE rejection and UTF-8-only rules.
- XML Elements Attributes Arrays to JSONUnderstand @attributes, #text, sibling arrays, empty elements, and namespace prefix preservation in the JSON output.
- How to Convert JSON to XMLConvert a .json file or pasted JSON into a formatted .xml document locally: choose a root element name, review the preview, and download without uploading.