XML to JSON

Convert XML to JSON: attributes become keys starting with @, text becomes #text and big integers stay exact.

loading XML to JSON
About this tool

What does an XML to JSON converter do?

XML is still everywhere that software is old, regulated or enterprise: SOAP services, RSS and Atom feeds, sitemaps, Maven and Android configuration, invoices such as the Brazilian NF-e, bank and payment messages, SVG. JSON is where modern code wants its data. Converting XML to JSON is how you read one in the other without writing a parser.

The two formats do not line up perfectly, and that is the interesting part. XML has attributes, element text, comments, namespaces and mixed content; JSON has objects, arrays, strings, numbers, booleans and null. Every converter must choose a convention. This one is explicit: an attribute becomes a key starting with @, the text of an element that also has attributes goes under #text, repeated sibling elements become an array, and an element with only text becomes a plain value.

It runs in your browser, so the XML you paste (an invoice, a SOAP response with personal data) is not uploaded. It reports malformed XML with the line and column, and you can switch off the type guessing when you need every value to stay text.

How to convert XML to JSON

  1. Paste the XML into the input box. The page opens with a small sample project and the direction (XML to JSON) is fixed at the top. The <?xml ...?> declaration is optional.
  2. The JSON appears next to it as you type. On a phone it sits below the input.
  3. Look at the checkbox that turns text like 12 and true into numbers and booleans. It is on by default. Turn it off when you want every value as a string, for example to keep 19.90 exactly as written.
  4. Choose the Indent (2 spaces, 4 spaces, tab), tick Sort keys for alphabetical order inside each object, or tick Minify for one line.
  5. Press Copy or Download to save converted.json.
  6. If the XML is malformed, a red box shows the line and column, the reason and the line itself, and the output returns when you fix it.

XML to JSON examples

Attributes, repeated elements and type guessing

Input:

<order id="1042">
  <customer><name>Maria Silva</name><zip>01310-100</zip><phone>0119999</phone></customer>
  <item sku="A1"><qty>2</qty><price>19.90</price></item>
  <item sku="B2"><qty>1</qty><price>5</price></item>
</order>

Output:

{
  "order": {
    "@id": 1042,
    "customer": { "name": "Maria Silva", "zip": "01310-100", "phone": "0119999" },
    "item": [
      { "@sku": "A1", "qty": 2, "price": 19.9 },
      { "@sku": "B2", "qty": 1, "price": 5 }
    ]
  }
}

The two <item> elements became an array and @id a number. The zip code and the phone stayed text: values with a leading zero or other characters, such as 01310-100, are treated as identifiers, not numbers. With the checkbox off, every value is a string, including "19.90".

A single element is not an array

In an RSS feed with two <item> blocks, item is an array. With only one, it is a single object. XML cannot say "this is a list" with one element, so your code should accept both shapes, for example by wrapping a lone object in an array.

Text plus attributes

<price currency="BRL">19.90</price> becomes {"price": {"@currency": "BRL", "#text": 19.9}}. Without attributes, the same element is just "price": 19.9.

Namespaces and CDATA

Prefixes are kept as part of the names: <soap:Envelope xmlns:soap="..."> becomes the key "soap:Envelope" with an "@xmlns:soap" entry. A <![CDATA[a < b & c]]> section becomes the plain string "a < b & c", and an empty element such as <empty/> becomes "".

Mixed content and a version number

<p>Hello <b>world</b>!</p> has text on both sides of a child element, so the result is {"p": {"#text": ["Hello", "!"], "b": "world"}} and the position of b between them is lost. Numbers that would not read back identically, such as version="2.0" or 1.50, stay as text; 3 and 2.5 become numbers. Turn the checkbox off to keep every value as text.

Common errors and how to fix them

  • Unexpected close tag (for example line 1, column 11 in <a><b></a>): a closing tag does not match the open one. Each <b> needs its own </b>, in the right order.
  • Invalid character in entity name (line 1, column 8 in <a>R&D</a>): a bare &. In XML write &amp;.
  • Unencoded < (in <a>1 < 2</a>): a bare < inside text. Write &lt;.
  • Unquoted attribute value (in <a x=1>): XML attributes always need quotes: x="1".
  • Text outside the root element (line 1, column 1 for just text): the input is not XML at all, or text sits before or after the root element. Paste only the document. Other structural errors, such as a tag never closed, a second root element or a repeated attribute, are reported with their line and column too.

Frequently asked questions

How do I convert XML to JSON online?

Paste the XML, check the output and press Copy or Download. It runs in your browser and needs no sign-up. To go back, use JSON to XML.

How are XML attributes represented in JSON?

As keys that begin with @, next to the child elements: <item sku="A1"> becomes {"@sku": "A1"}. If the element also has text, the text is stored as #text.

Are comments and the XML declaration kept?

No. Comments, processing instructions and the <?xml ...?> line are dropped, because JSON has nowhere to put them. The encoding is not part of the data either.

Can I format the XML first?

Yes. If the XML is hard to read, run it through the XML formatter to see its structure, then convert it. The data converter also reads CSV, YAML and TOML.

Why did a number become a string, or the reverse?

By default, text that looks like a decimal number or true or false is converted, except when it has a leading zero, a plus sign or other characters. Turn the type guessing off to keep everything as text, and then convert the few values you need in your own code.

How to convert XML to JSON in code

JavaScript

fast-xml-parser works in Node and in the browser; options change how attributes are named.

import { XMLParser } from 'fast-xml-parser';

const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@' });
console.log(JSON.stringify(parser.parse('<a id="1"><b>x</b></a>'), null, 2));

Python

xmltodict uses the same @ convention for attributes and #text for text.

import json
import xmltodict

with open("order.xml", "rb") as f:
    data = xmltodict.parse(f)
print(json.dumps(data, indent=2, ensure_ascii=False))

Unlike this page, xmltodict keeps every value as a string.

Bash

python3 -c 'import sys,json,xmltodict; print(json.dumps(xmltodict.parse(sys.stdin.read())))' < order.xml
Report a problem