XML to JSON Converter

Convert XML to JSON and choose how attributes, repeated elements and namespaces are mapped. It runs entirely in your browser, so your XML is never uploaded.

The converter runs entirely in your browser and needs JavaScript. Everything else on this page works without it. Alternatives: the xmltodict library in Python, or fast-xml-parser in Node.js.

Paste XML, drop a file or load the example. Files are read in your browser: nothing is uploaded. Press Ctrl+Enter to convert.

Result

JSON with line numbers. Open Options to change how attributes, text and arrays are written.

The JSON will appear here.

Did this tool help? Support XMLNest

How do I convert XML to JSON online?

Paste your XML into the input box, open a file with Open file… or drop one onto the tool. Then press Convert, or Ctrl+Enter (⌘+Enter on a Mac). The JSON appears in the Result box with line numbers and colours. Copy it, or download it as a .json file named after your XML file. Load example shows a small book catalog first.

How does XML map to JSON?

The root element becomes the only key of the JSON object. An element that holds plain text becomes a string. An element with attributes or child elements becomes an object: attributes come first, with an @ before their name, then the children in the order they first appear. When an element has both attributes and text, the text goes under #text. Child elements that share a name are collected into an array, even when other elements sit between them. Comments, processing instructions and the XML declaration are left out.

Attributes, arrays and namespaces: which options do I need?

Open Options when the code that reads the JSON expects another shape. An underscore prefix, or no prefix at all, gives keys that are easier to type in JavaScript. Arrays set to Always turns every child element into an array: a list with one item then has the same shape as a list with ten. Remove prefixes turns g:price into price. Numbers and true/false writes 42 and true without quotes; leave it off when postal codes or IDs must stay text.

Does this converter upload my file?

No. The conversion runs in this browser tab. Your file is read on your device, and the security rules of this page stop it from sending anything over the network. Nothing is saved, so your XML is gone when you close the tab. If the XML is broken, you get the first error with its line and column, as in the XML validator, and Go to error selects it in the input.

Converting XML to JSON in code

In a project, a library does the same job: xml2js or fast-xml-parser in Node.js, xmltodict in Python, Jackson in Java. Each has its own conventions for attributes and arrays, so compare a sample of its output with the result here before you rely on it. To tidy the XML itself first, use the XML formatter.

Questions

How are XML attributes converted to JSON?

Each attribute becomes a key that starts with @, with its value as a string, for example "@id": "bk101". In Options, you can use an underscore instead, or no prefix at all.

Why is one element an array and another one not?

By default, only elements that repeat inside the same parent become arrays. If your code needs the same shape every time, set Arrays to Always.

What happens to namespaces?

Prefixes stay as written, such as dc:title, and the xmlns declarations appear as attributes. Choose Remove prefixes in Options to get plain names.

Is there a file size limit?

Yes, 20 MB for an opened file or for pasted text. The conversion runs in the background, so the page stays responsive while it works.

Can it convert JSON back to XML?

Not yet. This tool only converts XML to JSON. A JSON to XML converter may come later, with the other tools on this site.

Can I look at an XML file in Chrome before converting it?

Yes. Chrome shows an XML file that has no stylesheet as a plain tree. XMLNest, an extension available on the Chrome Web Store, adds folding, line numbers and a cleaner layout, which makes a large file easier to read.