XML to JSON
Convert an XML document into JSON, and decide how attributes, text and repeated elements are shaped.
What this tool does
XML to JSON reads your document with a parser written for this page, then flattens each element into a plain object. An element holding nothing but text becomes a string. Attributes become keys carrying a prefix, so id="7" arrives as "@id": "7" — they are not gathered into a nested container the way xml2js collects them under $, so output from this page is not a drop-in match for that library. An element with both attributes and text keeps its text under a separate key, #text by default. Repeated sibling elements collapse into an array.
That last rule is the one to settle before you write code against the output. Two <line> elements become an array; a single <line> element becomes one object, so the same schema produces two different shapes depending on how much data the document happened to carry. Switching on Always use arrays for child elements wraps every child regardless, which is usually what the code reading the JSON actually wants.
Some of the XML does not survive the trip, and it is worth knowing which parts. Comments, processing instructions and the XML declaration are dropped. CDATA sections come through as ordinary text with their markers removed. Namespace prefixes are kept verbatim inside the key, so <ns:item> becomes "ns:item" and xmlns declarations arrive as ordinary attributes — nothing is resolved against a namespace URI. Entities declared inside a DOCTYPE are not expanded either; only the standard XML entities, a short list of common named ones and numeric references such as — are decoded.
The work happens in this browser tab, and the document is not sent anywhere. That matters for XML in particular, because it is the format that still carries invoices, patient records, bank statements and SOAP payloads out of systems that were never meant to be pasted into a stranger's website. The practical ceiling is your own device: the whole document is parsed in memory, and there is no file picker here, so you paste the text rather than opening a file. A few megabytes is comfortable; a very large export will make the tab work for it.
How to use it
- Paste the XML. Type or paste the document into the input box, or press Load sample for an order with attributes, repeated elements and a CDATA section.
- Name the attribute and text keys. Attribute prefix defaults to @ and the text-content key to #text. Change either when the code consuming the JSON expects different names. Emptying a box does not strip the prefix or the key — a blank field falls back to the default.
- Choose the shape. Drop the root element wrapper removes the outer key. Always use arrays for child elements gives every child a consistent array, whatever the document contained.
- Set casting, trimming and indentation. Number and true/false casting is off by default. Trimming whitespace around text is on. Indent with 2 spaces, 4 spaces, a tab, or none for a single line.
Processing & privacy
Runs on your device. Your file is processed directly in this browser.
Common use cases
Reading a SOAP or feed response
A wall of angle brackets is hard to scan. As JSON the nesting is obvious, and you can see straight away whether the field you expected actually arrived.
Moving a legacy export into a JavaScript pipeline
Older systems still emit XML by default. Converting once gives you something JSON.parse, jq and a test fixture can all read without a dependency.
Locating a break in a malformed document
The parser stops at the first structural problem and names the line and column. A closing tag that does not match its opening one is pinpointed exactly; an element that is simply never closed reports the end of the document, so you still have to look back for where it opened.
Building a stable fixture from sample data
Enable Always use arrays so a one-item response and a fifty-item response produce the same shape, and your test stops depending on how busy the day was.
Questions
Is my XML uploaded?
Why is one element an object and another an array?
What happens to mixed content?
Why did my numbers come out as strings?
What does it do with an invalid document?
Can I convert the result back to XML?
Should I use this or XML Formatter?
Related tools
- JSON to CSV — Flatten a JSON array into a CSV table.
- CSV to JSON — Turn a CSV file into structured JSON.
- CSV Viewer — Open and browse a large CSV without a spreadsheet app.
- CSV to Excel — Convert CSV files into an XLSX workbook.
- Excel to CSV — Export spreadsheet sheets to CSV.
- CSV Merge — Combine multiple CSV files with matching columns.