# JSON vs XML: Size and Speed on Real Records

> JSON vs XML on the same 100 real records: 41,810 bytes against 61,929, parse times in Python and Node, what each format keeps, and when to choose which.

[Home](https://quanticdata.io/) / [Blog](https://quanticdata.io/blog/) / JSON vs XML: Size and Speed on Real Records

# JSON vs XML: the difference, measured on the same 100 real records

Web scraping · Nov 20, 2026 · 7 min read · By [Aldo Morese](https://quanticdata.io/about/), founder of QuanticData

![The same 100 real package records written as JSON and as XML on 20 November 2026: 41,810 bytes as JSON, 48,039 bytes as XML with attributes and 61,929 bytes as XML with one element per field; the card shows the Python parse times of the run, 0.200 ms for json.loads and 0.264 ms for lxml, and that JSON kept the numbers as numbers.](https://quanticdata.io/blog/json-vs-xml/hero.webp?v=20261120h)

On this page

- [JSON vs XML at a glance](https://quanticdata.io/blog/json-vs-xml/#json-vs-xml-at-a-glance)
- [The same record in JSON and in XML](https://quanticdata.io/blog/json-vs-xml/#the-same-record-in-json-and-in-xml)
- [JSON vs XML size: 41,810 bytes against 61,929](https://quanticdata.io/blog/json-vs-xml/#json-vs-xml-size-41-810-bytes-against-61-929)
- [JSON vs XML performance: parse times in Python and Node](https://quanticdata.io/blog/json-vs-xml/#json-vs-xml-performance-parse-times-in-python-and-node)
- [What each format keeps: types, lists and null](https://quanticdata.io/blog/json-vs-xml/#what-each-format-keeps-types-lists-and-null)
- [What XML can do that JSON cannot](https://quanticdata.io/blog/json-vs-xml/#what-xml-can-do-that-json-cannot)
- [JSON vs XML: which to choose](https://quanticdata.io/blog/json-vs-xml/#json-vs-xml-which-to-choose)

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.

**Run it on the free credit.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-lead&utm_content=json-vs-xml) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

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](https://quanticdata.io/collectors/npm-registry-api/): 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.

**Try it on your own targets.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-inline&utm_content=json-vs-xml) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

## 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:

```python
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:

```javascript
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](https://quanticdata.io/blog/parse-json-python/) and [parsing XML in Python](https://quanticdata.io/blog/parse-xml-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](https://quanticdata.io/blog/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 `rank` from an attribute.
- **Validation in the parser.** lxml checked a record against a small XSD schema and named the offending element when `rank` was "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](https://quanticdata.io/blog/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](https://quanticdata.io/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](https://quanticdata.io/blog/read-json-file-javascript/) covers the reading side.

### Sources & further reading

- [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259)
- [W3C: Extensible Markup Language (XML) 1.0](https://www.w3.org/TR/xml/)
- [Python documentation: json](https://docs.python.org/3/library/json.html)
- [Python documentation: xml.etree.ElementTree](https://docs.python.org/3/library/xml.etree.elementtree.html)
- [MDN: JSON.parse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)

## FAQ

Quick answers on json vs xml.

[Something else? Ask us](mailto:hello@quanticdata.io)

### What is the difference between JSON and XML?

JSON writes data as objects, lists, strings, numbers, booleans and null. XML wraps each value in named opening and closing tags and adds attributes, comments, namespaces and schema validation. JSON is smaller and maps straight to program objects; XML can describe documents and validate itself.

### Is JSON smaller than XML?

For our 100 records, yes: 41,810 bytes as JSON against 61,929 bytes as XML with one element per field, and 48,039 bytes with attributes. After gzip the files were 7,921 and 8,456 bytes, a 7% difference.

### Is JSON faster than XML?

In our run JSON parsed faster in each pair: 0.200 ms against 0.264 ms with lxml and 0.444 ms with ElementTree in Python, and 0.086 ms against 2.325 ms with a JavaScript DOM parser in Node. The gap depended mostly on the XML parser used.

### Which is better, JSON or XML?

JSON for APIs, configuration and data read by code, because types and lists survive the round trip. XML for documents, feeds, sitemaps and exchanges that require validation against a published schema.

### Does XML have data types?

Not in the file itself: every value is text. In our test the number 252786003 and the boolean false both came back as strings. Types are added by a schema such as XSD, or by your own conversion code.

### Can you put comments in JSON?

No. Python's `json.loads` rejected a JSON text containing a comment with a syntax error in our test, while the XML parser accepted an XML comment.

## Web data as typed JSON, ready to load

Collectors and the Web Scraping API return structured JSON records from public pages.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-end&utm_content=json-vs-xml) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

## Related reading

- [Web Scraping Project Ideas, Started for Real](https://quanticdata.io/blog/web-scraping-project-ideas/): Web scraping · Five web scraping project ideas, each one started for real on 20 November 2026: a price tracker, a library release dashboard, a question analysis, an open-source landscape and a topic timeline, with the code, the first run and what to build next.
- [Retry-After Header: Read It, Wait, Retry](https://quanticdata.io/blog/retry-after-header/): Web scraping · Retry-After tells a client how long to wait before the next request, as a number of seconds or as an HTTP date. Below: both formats, a Python and a Node helper we ran against a real 429 on 20 November 2026 (each waited 10 seconds and then got 200), the rate-limit headers of the QuanticData API, and how one batch job replaced 20 calls.
- [How to Use an API Key: curl, Python and JS](https://quanticdata.io/blog/how-to-use-an-api-key/): Web scraping · An API key is a secret string you send with every request so the service knows which account is calling. Nothing is installed: you add one header. Below: the same call in curl, Python, JavaScript and PowerShell, run on 20 November 2026 against the QuanticData API, the exact 401 messages for the usual mistakes, and how to keep the key out of your code.

---

Source: https://quanticdata.io/blog/json-vs-xml/ · Site index for AI: https://quanticdata.io/llms.txt
