Documentation Python quickstart Blog Free tools Enterprise solutions hello@quanticdata.ioLog in

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

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.
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.

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

QuestionJSONXML
Size of our 100 records41,810 bytes61,929 bytes (48,039 with attributes)
After gzip7,921 bytes8,456 bytes
Data typesString, number, boolean, null, list, objectText; types come from a schema or from your code
ListsBuilt in: [...]Repeated elements
CommentsNot allowedAllowed
Attributes, namespaces, mixed text and tagsNoYes
ValidationJSON Schema (separate specification)XSD and DTD, built into common parsers
Where you meet itREST APIs, config files, logsSitemaps, 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
RuntimeJSON parserXML parserMedian, JSONMedian, XML
Python 3.12.10json.loadslxml.etree.fromstring0.200 ms0.264 ms
Python 3.12.10json.loadsElementTree.fromstring0.200 ms0.444 ms
Node.js v22.23.2JSON.parseDOMParser from @xmldom/xmldom0.086 ms2.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 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 caseChooseWhy, from the run above
A web or mobile API, a scraper's output, a config file read by codeJSONTypes and lists survive the round trip; one call gives you records
Data that goes into JavaScriptJSONJSON.parse is native: 0.086 ms for our file
Documents, text with markup, feeds, sitemapsXMLMixed content, attributes and comments
A partner or a standard that publishes an XSDXMLThe parser validates each file against the schema
Flat rows for a spreadsheetNeitherSee 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.

Sources & further reading

FAQ

Quick answers on json vs xml.

Something else? Ask us

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.

Related reading