XML to JSON

Convert an XML document into JSON, and decide how attributes, text and repeated elements are shaped.

Runs on your deviceDataWorkingNo account needed

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 &#8212; 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

  1. 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.
  2. 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.
  3. 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.
  4. 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?
No. Parsing and serialising both happen in this browser tab, so the document is not transmitted, logged or stored. The tool still works with the network switched off.
Why is one element an object and another an array?
Because a name that appears once becomes a single value and a name that repeats becomes an array. It is the standard XML-to-JSON ambiguity: nothing in the document says whether a list is meant. Turn on Always use arrays for child elements to remove the guesswork.
What happens to mixed content?
Text that surrounds child elements is collected and joined under the text key, so its position relative to those children is lost. <p>Hello <b>world</b> again</p> tells you the text and the child element, but not that the text sat on either side of it. Prose-style markup such as XHTML fragments is the wrong input for this tool.
Why did my numbers come out as strings?
Casting is off unless you enable it, so part codes, phone numbers and account references keep their exact text. Even with casting on, values with leading zeros such as 007 stay strings, and values beyond roughly 15 digits stay text rather than losing precision as JavaScript numbers.
What does it do with an invalid document?
It refuses, and it says where. A closing tag that does not match the open one, an attribute value without quotes, a second root element or an unterminated comment each produce a message naming the line and column at which the parser gave up.
Can I convert the result back to XML?
JSON to XML makes the opposite trip, but the round trip is not lossless. Anything dropped on the way in — comments, the declaration, mixed-content ordering — cannot be put back on the way out.
Should I use this or XML Formatter?
Use XML Formatter when you want to keep the document as XML and only make it readable. Use this when something downstream needs JSON.

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.