Which Python JSON Library Is Fastest? ujson vs rapidjson vs orjson

This article compares the performance of Python's built‑in json module with popular third‑party libraries ujson, rapidjson, and the Rust‑based orjson, showing benchmark results, installation tips, code examples, and known bugs to help developers choose the most efficient JSON serializer.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Which Python JSON Library Is Fastest? ujson vs rapidjson vs orjson

When using json.dumps(data) in Python, the author noticed significant slowdown even though the data size was modest but consisted of a large list of elements, prompting a search for faster JSON handling libraries.

The comparison focused on ujson (UltraJSON, written in pure C), python-rapidjson (C++ wrapper for RapidJSON), and the built‑in json module, with a brief mention of simplejson and the Rust‑based orjson discovered later.

Both ujson and rapidjson rely on traditional C/C++ for speed; their loads() performance is similar, but dumps() on large objects reveals the built‑in json library to be considerably slower.

Installation commands:

pip install python-rapidjson pip install simplejson

The author tested ujson, rapidjson, and the built‑in json using the following script (saved as test.py) to measure serialization time:

from time import time
import sys, string
num = int(sys.argv[1])
lib = sys.argv[2]
items = []
for i in range(num):
    items.append({c: c for c in string.ascii_letters})
start = time()
if lib == 'ujson':
    import ujson
    ujson.dumps(items)
elif lib == 'rapidjson':
    import rapidjson
    rapidjson.dumps(items)
elif lib == 'orjson':
    import orjson
    orjson.dumps(items)
else:
    import json
    json.dumps(items)
print(time() - start)

Run the script with commands like python 1000|10000|100000|1000000 json|ujson|rapidjson|orjson and record the execution times. The results matched the figures from the "Benchmark of Python JSON libraries" article, as shown in the following images:

Further investigation of the UltraJSON GitHub page revealed benchmarks where orjson (written in Rust) and nujson outperformed ujson. The author therefore added orjson to the test script and obtained additional data:

Unlike the other libraries, orjson 's dumps() does not accept the indent argument and returns bytes, so formatting requires decoding, e.g.:

import orjson
json_str = orjson.dumps(record, option=orjson.OPT_INDENT_2).decode()

The author also documented a bug in ujson versions 3.0.0 and 3.1.0 where the indent parameter is ignored beyond a value of 1, showing example REPL output. Using ujson==2.0.3 restores proper indentation but lacks support for serializing datetime objects.

In summary, for high‑performance JSON serialization in Python, orjson (Rust‑based) generally offers the best speed, followed by ujson (C‑based) and rapidjson (C++‑based), while the built‑in json module should be reserved for cases where maximum compatibility is required.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

serializationRapidJSONujsonorjson
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.