JSON and XML are both text formats for structured data. JSON writes objects, lists, strings, numbers, booleans and null with very little punctuation, which is why most web APIs use it. XML wraps every value in named tags and adds attributes, comments, namespaces and schema validation, which is why documents, feeds and sitemaps use it.
To put numbers on the difference, we took 100 real records on 20 November 2026, wrote them once as JSON and once as XML, and measured both files and both parsers in Python and Node.js. Everything below is the output of that run.
JSON vs XML at a glance
| Question | JSON | XML |
|---|---|---|
| Size of our 100 records | 41,810 bytes | 61,929 bytes (48,039 with attributes) |
| After gzip | 7,921 bytes | 8,456 bytes |
| Data types | String, number, boolean, null, list, object | Text; types come from a schema or from your code |
| Lists | Built in: [...] | Repeated elements |
| Comments | Not allowed | Allowed |
| Attributes, namespaces, mixed text and tags | No | Yes |
| Validation | JSON Schema (separate specification) | XSD and DTD, built into common parsers |
| Where you meet it | REST APIs, config files, logs | Sitemaps, RSS and Atom feeds, SOAP, office documents, SVG |
The same record in JSON and in XML
The records come from our npm registry collector: we searched the npm registry for "html parser" and kept 13 fields of each of the 100 packages returned, one of them a list of keywords. This is the first record in JSON:
{"name": "parse5", "rank": 1, "version": "8.1.1", "license": "MIT",
"description": "HTML parser and serializer.",
"keywords": ["html", "parser", "html5", "WHATWG", "specification", ...],
"homepage": "https://parse5.js.org", "insecure": false,
"dependents": 3075, "downloads_weekly": 252786003, ...}
And the same record in XML, one element per field:
<packages>
<package>
<name>parse5</name>
<rank>1</rank>
<version>8.1.1</version>
<license>MIT</license>
<description>HTML parser and serializer.</description>
<keywords>
<keyword>html</keyword>
<keyword>parser</keyword>
...
</keywords>
<homepage>https://parse5.js.org</homepage>
<insecure>false</insecure>
<dependents>3075</dependents>
<downloads_weekly>252786003</downloads_weekly>
...
Two differences are visible before any measurement. Every XML field name is written twice, once to open and once to close. And JSON says what each value is: 1 is a number, false a boolean, [...] a list. In XML they are all text between tags.
JSON vs XML size: 41,810 bytes against 61,929
We wrote the 100 records five ways and compressed each file with gzip at level 9:
file bytes gzip
packages.json 41,810 7,921
packages.pretty.json 54,120 8,229
packages.xml 61,929 8,456
packages.pretty.xml 72,939 8,611
packages.attr.xml 48,039 8,099
records 100 keywords total 507
- Without compression, the XML file was 48% larger than the JSON file: 61,929 bytes against 41,810. The closing tags and the 507
<keyword>elements are most of the gap. - XML with attributes was closer. Writing the simple fields as attributes (
<package name="parse5" rank="1" ...>) brought it to 48,039 bytes, 15% above JSON. How you design the XML matters as much as the choice of format. - Gzip closed most of the gap. Repeated tag names compress very well: 8,456 bytes for XML against 7,921 for JSON, a 7% difference. Most HTTP responses travel compressed, so on the wire the two formats were close in this run.
- Indentation costs more than you might expect before compression: pretty-printed JSON was 54,120 bytes, 29% above the compact file.
JSON vs XML performance: parse times in Python and Node
We parsed each file 300 times and took the median, on one Windows 11 PC. Python 3.12.10 with the standard library and lxml 6.1.1:
import json, xml.etree.ElementTree as ET
from lxml import etree as LX
json.loads(jb) # jb = bytes of packages.json
ET.fromstring(xb) # xb = bytes of packages.xml
LX.fromstring(xb)
python 3.12.10 lxml 6.1.1 (median of 300 runs, ms)
json.loads 0.200
ElementTree.fromstring 0.444
lxml.etree.fromstring 0.264
ElementTree + rebuild dicts 0.574
Node.js v22.23.2, with the built-in JSON.parse and the @xmldom/xmldom 0.8.15 package for XML:
const data = JSON.parse(js);
const doc = new DOMParser().parseFromString(xs, 'text/xml');
node v22.23.2 @xmldom/xmldom 0.8.15 (median of 300 runs, ms)
JSON.parse 0.086
DOMParser (xmldom) 2.325
| Runtime | JSON parser | XML parser | Median, JSON | Median, XML |
|---|---|---|---|---|
| Python 3.12.10 | json.loads | lxml.etree.fromstring | 0.200 ms | 0.264 ms |
| Python 3.12.10 | json.loads | ElementTree.fromstring | 0.200 ms | 0.444 ms |
| Node.js v22.23.2 | JSON.parse | DOMParser from @xmldom/xmldom | 0.086 ms | 2.325 ms |
Three things to take from this. JSON was faster in each pair we ran. The size of the gap depended on the XML parser far more than on XML itself: lxml, written in C, was within a third of json.loads, while a DOM parser written in JavaScript took 27 times as long as the engine's native JSON.parse. And all of these are fractions of a millisecond to a few milliseconds for 100 records; a network request for the same file takes far longer than either parser. This is one run on one machine with one dataset: treat the ratios as an example, not a law.
There is a second cost the first three lines do not show. After json.loads you already have a list of dictionaries. After ET.fromstring you have a tree, and turning it back into records is your code: in our run that step raised ElementTree from 0.444 ms to 0.574 ms. The how-to for each side is in parsing JSON in Python and parsing XML in Python.
What each format keeps: types, lists and null
We parsed both files back and printed the type of the same fields of the first record:
name json str 'parse5' xml str 'parse5'
rank json int '1' xml str '1'
insecure json bool 'False' xml str 'false'
downloads_weekly json int '252786003' xml str '252786003'
quality_score json NoneType 'None' xml NoneType 'None'
keywords json list "['html', ..." xml list "['html', ..."
JSON returned the number as a number and the boolean as a boolean. XML returned text for both, and the list is a list only because our code collected the <keyword> elements into one: a package with a single keyword gives one element, and nothing in the file says whether that is a list of one or a single value. The same trap in Node:
json first name: parse5 | weekly downloads + 1 = 252786004
xml first name: parse5 | weekly downloads + 1 = 2527860031 | Number() + 1 = 252786004
Adding 1 to the XML value appended the character "1" to the string. With XML, converting types is part of data parsing; with JSON the format has done it for the six types it knows. Neither format has a date type: published_at came back as a string from both.
What XML can do that JSON cannot
XML is the larger format because it is the more capable one. Three checks from the same run:
JSON with a comment -> Expecting property name enclosed in double quotes: line 1 column 20 (char 19)
XML with a comment -> parsed, name = parse5 , rank attribute = 1
XSD validate good -> True
XSD validate bad -> False | Element 'rank': 'first' is not a valid value of the atomic type 'xs:positiveInteger'.
mixed content -> 'A ' 'very fast' ' HTML parser.'
- Comments and attributes. A comment inside JSON is a syntax error; XML parsed the comment and read
rankfrom an attribute. - Validation in the parser. lxml checked a record against a small XSD schema and named the offending element when
rankwas "first" instead of a positive integer. This is how XML gets its types back, and why contracts between organisations often stay on XML. - Mixed content.
<p>A <b>very fast</b> HTML parser.</p>keeps text and markup in order. That is what documents need, and it has no natural form in JSON.
JSON vs XML: which to choose
| Your case | Choose | Why, from the run above |
|---|---|---|
| A web or mobile API, a scraper's output, a config file read by code | JSON | Types and lists survive the round trip; one call gives you records |
| Data that goes into JavaScript | JSON | JSON.parse is native: 0.086 ms for our file |
| Documents, text with markup, feeds, sitemaps | XML | Mixed content, attributes and comments |
| A partner or a standard that publishes an XSD | XML | The parser validates each file against the schema |
| Flat rows for a spreadsheet | Neither | See CSV vs JSON |
In web data work you usually do not choose: the source does. APIs answer in JSON, sitemaps and feeds are XML, and pages are HTML that you parse into JSON yourself. Our Web Scraping API and collectors return JSON, so the records in this post arrived typed and ready to load; in Node, reading a JSON file in JavaScript covers the reading side.


