Skip to the content

Contact usFree tools · No signup · Files stay on your device

Developer Tools

XML to JSON: the complete guide to converting XML data

XML to JSON: the complete guide to converting XML data

XML and JSON can describe the same data, but they say it differently. XML has tags, attributes and text sitting side by side; JSON has objects, arrays and plain values. Converting between them means making a set of choices, and a good converter lets you make those choices yourself. Our free XML to JSON converter does exactly that: you pick how attributes, repeated elements and namespace prefixes are written, and it shows any XML mistake with its line and column.

This guide is for developers moving a SOAP or feed response into a web app, testers comparing an XML export with a JSON API, and anyone who has received an XML file and needs something easier to work with. Everything runs in your browser.

What XML to JSON does

It reads your XML, checks that it is well-formed, and writes JSON that follows a clear set of rules. The root element always stays as the single top-level key, so nothing about the document's name is lost.

It does not validate against a schema, and it does not guess types unless you ask it to. If the XML has a syntax error, you get the error instead of output.

How to use XML to JSON, step by step

  1. Open XML to JSON.
  2. Paste XML into the box labelled "Paste XML or open an .xml file", or press Open file, or Sample.
  3. Under Options, pick a Convention, an Attributes style and the Namespace prefixes setting.
  4. Choose an Indent, and tick Convert numbers, booleans & null and Trim whitespace in text if you want them.
  5. Read the result in the "JSON output" panel. The line under it shows Elements, Attributes, Depth and Output size.
  6. Press Copy, or Download .json. The file is named after the one you opened, or data.json.

The rules, in one table

In the XML In the JSON
Attribute id="7" "@id": "7"
Element with only text A plain string
Element with text and attributes or children The text goes in a "#text" key
Empty element such as <gift/> null
A child name that appears more than once An array
Comment or processing instruction Left out

A worked example

Here is an order file from a small Dhaka shop:

<?xml version="1.0" encoding="UTF-8"?>
<orders xmlns:bd="http://example.com/bd" date="2026-11-20">
  <!-- Dhaka branch -->
  <order id="1001" paid="true">
    <bd:customer>Rahim Uddin</bd:customer>
    <phone>01712345678</phone>
    <item sku="A1" qty="2">Panjabi</item>
    <item sku="B7" qty="1">Sandals</item>
    <total currency="BDT">2101.50</total>
    <note>Call <b>before</b> delivery</note>
    <gift/>
  </order>
</orders>

With the settings the page opens with (Simple, @name keys, Keep namespace prefixes, Trim whitespace on, Convert numbers off) the JSON is:

{
  "orders": {
    "@xmlns:bd": "http://example.com/bd",
    "@date": "2026-11-20",
    "order": {
      "@id": "1001",
      "@paid": "true",
      "bd:customer": "Rahim Uddin",
      "phone": "01712345678",
      "item": [
        { "@sku": "A1", "@qty": "2", "#text": "Panjabi" },
        { "@sku": "B7", "@qty": "1", "#text": "Sandals" }
      ],
      "total": { "@currency": "BDT", "#text": "2101.50" },
      "note": { "#text": "Call delivery", "b": "before" },
      "gift": null
    }
  }
}

(The tool prints each object over several lines; they are joined here to save space.) A few things to notice. The two item tags became an array. The phone number kept its leading zero, because it is still text. The comment is gone, and a note under the panels says comments and processing instructions were left out. And the note element lost its word order: the text "Call" and "delivery" are joined into one #text, with b beside it, because JSON objects cannot interleave text and tags.

Every option explained

Convention: Simple or Always arrays

Simple makes a name that repeats into an array and leaves a single name as a single value. It gives the most compact JSON, but the shape depends on the data: an order with one item has an object where an order with two has an array.

Always arrays wraps every child element in an array, even when it occurs once. In our example, "phone": ["01712345678"] and "order": [ { ... } ]. Choose it when code reads the JSON and you do not want to write "is this an array or not?" checks. Attributes and text are not wrapped.

Attributes: @name keys, _name keys or One $attrs object

This only changes how attributes are named. @name keys gives "@id", _name keys gives "_id", and One $attrs object collects them: "$attrs": { "id": "1001", "paid": "true" }. Pick the style your downstream code expects. The @ style is the most widely used, and it is the one JSON to XML expects by default.

Namespace prefixes: Keep or Drop

Keep writes names exactly as in the XML, such as bd:customer, and keeps the xmlns:bd declaration as an attribute. Drop removes the prefix, giving customer, and removes the xmlns declarations. Dropping can cause collisions: if a:id and b:id both exist on one tag they become the same key, and the later value wins. A note reports how many such collisions happened.

Convert numbers, booleans & null

Off by default, because XML has no types and a value such as 01712345678 is an identifier, not a number. When ticked, plain numbers, true, false and null become JSON values. In our example "@id": 1001, "@paid": true and "#text": 2101.5 (note the trailing zero is gone). Values with a leading zero, such as 007, stay text, and so do integers too big to be exact (over 9,007,199,254,740,991). The conversion applies to attribute values as well as text.

Trim whitespace in text

On by default. It removes spaces and line breaks around text, so indented XML does not leave spaces in your strings. Turn it off if the spaces matter, for example " hi " stays as it was.

Indent

Choose 2 spaces, 4 spaces, Tabs or Minified. Minified gives one line, which is handy for pasting into a request.

What it accepts and its limits

  • Input: well-formed XML, up to 10 MB, nested no more than 512 levels. Longer or deeper input is refused with a message.
  • Files: Open file accepts .xml, .svg, .rss, .atom, .xhtml and .txt.
  • Large input: above 1 MB the page waits for you to press Convert instead of converting as you type.
  • Display: the panel shows the first 200,000 characters of a very large result. Copy and Download .json give everything.
  • CDATA: treated as ordinary text. A DOCTYPE is accepted, but entities it declares are never expanded.

Privacy

XML to JSON runs in your browser, and your XML is not sent to our server. The parser never loads external files and never expands custom entities, so a document from an untrusted source cannot make it fetch anything.

What JSON cannot keep

Be aware of what is lost on the way. Comments and processing instructions are dropped. Siblings with the same name are grouped under one key, so if item and note tags alternate in the XML, that alternation is lost. And mixed content, text between tags, is flattened as in the note example. If any of that matters, keep the XML as your source of truth.

Common problems and how to fix them

"Line 3, column 9" and an error message

The XML is not well-formed. Press Go to error to select the spot. The messages are the same as in the formatter, and the most common ones are explained in the XML Formatter guide: a closing tag that does not match, an unescaped &, and attribute values without quotes.

The shape changes between two files

You are on Simple, and one file has a single record where the other has several. Switch to Always arrays.

A field I expect is nested under "#text"

That element has an attribute or a child as well as text, so the text needs its own key. In code, read ["#text"].

The JSON has a lot of @ keys I do not want

Change the Attributes setting to _name keys, or to One $attrs object so they are grouped in one place.

Quick checklist

  • Use Simple for a quick look and Always arrays for code.
  • Tick Convert numbers only if no field is an identifier with leading zeros.
  • Keep prefixes unless you are sure there are no clashes.
  • Keep the XML; comments and sibling order do not survive.

Frequently asked questions

Why do some of my keys start with @?
They are XML attributes. XML has two places to put data, attributes and child elements, but JSON has one, so attributes are marked with a prefix. The default is @, so id="1001" becomes "@id": "1001". You can switch to _ or to one $attrs object.
Why is a repeated tag sometimes an array and sometimes not?
In the Simple convention a name that appears once is a single value and a name that appears twice or more is an array, so the shape follows your data. Choose Always arrays if your code needs the same shape every time.
Why are my numbers still strings in the JSON?
XML has no number type, so by default every value stays text. Tick Convert numbers, booleans & null to get real JSON values. Values like 007 and integers too large to be exact stay as text on purpose.
What happens to comments and processing instructions?
They are left out, because JSON has nowhere to put them. A note under the output tells you when something was dropped.
Can I get the XML back from the JSON?
For the common cases, yes. JSON to XML reads the same conventions, with @ for attributes and #text for text. What cannot come back is the order of mixed text and tags, comments and anything the converter had to drop.
Found this useful? Share it

Tools in this guide

Main tool

XML to JSON

Paste XML or open a file and get clean JSON.

Open tool

JSON to XML

Paste JSON and get well-formed XML with an optional declaration and your choice of indent.

Open tool

XML Formatter

Paste XML to check that it is well-formed and re-print it cleanly, or squeeze it down to one line.

Open tool

JSON Formatter

Paste messy or minified JSON to get clean, indented output instantly — or minify it for production.

Open tool

Back to all guides & blog