Introduction
marketing campaign you arrange for Black Friday was a large success, and prospects begin pouring into your web site. Your Mixpanel setup which might normally have round 1000 buyer occasions an hour finally ends up having hundreds of thousands of buyer occasions inside an hour. Thereby, your information pipeline is now tasked with parsing huge quantities of JSON information and storing it in your database. You see that your customary JSON parsing library just isn’t in a position to scale as much as the sudden information progress, and your close to real-time analytics studies fall behind. That is once you understand the significance of an environment friendly JSON parsing library. Along with dealing with massive payloads, JSON parsing libraries ought to have the ability to serialize and deserialize extremely nested JSON payloads.
On this article, we discover Python parsing libraries for big payloads. We particularly take a look at the capabilities of ujson, orjson, and ijson. We then benchmark the usual JSON library (stdlib/json), ujson, and orjson for serialization and deserialization efficiency. As we use the phrases serialization and deserialization all through the article, right here’s a refresher on the ideas. Serialization entails changing your Python objects to a JSON string, whereas Deserialization entails rebuilding the JSON string out of your Python information buildings.
As we progress by means of the article, you can see a call circulate diagram to assist determine on the parser to make use of based mostly in your workflow and distinctive parsing wants. Along with this, we additionally discover NDJSON and libraries to parse NDJSON payloads. Let’s get began.
Stdlib JSON
Stdlib JSON helps serialization for all fundamental Python information varieties, together with dicts, lists, and tuples. When the perform json.masses() is named, it masses your entire JSON into reminiscence without delay. That is wonderful for smaller payloads, however for bigger payloads, json.masses() could cause crucial efficiency points akin to out-of-memory errors and choking of downstream workflows.
import json
with open("large_payload.json", "r") as f:
json_data = json.masses(f) #masses complete file into reminiscence, all tokens without delay
ijson
For payloads which are within the order of a whole bunch of MBs, it’s advisable to make use of ijson. ijson, quick for ‘iterative json’, reads recordsdata one token at a time with out the reminiscence overhead. Within the code under, we evaluate json and ijson.
#The ijson library reads information one token at a time
import ijson
with open("json_data.json", "r") as f:
for report in ijson.gadgets(f, "gadgets.merchandise"): #fetch one dict from the array
course of(report)
As you may see, ijson fetches one aspect at a time from the JSON and masses it right into a Python dict object. That is then fed to the calling perform, on this case, the method(report) perform. The general working of ijson has been supplied within the illustration under.

ujson

Ujson has been a extensively used library in lots of purposes involving massive JSON payloads, because it was designed to be a sooner different to the stdlib JSON in Python. The velocity of parsing is nice for the reason that underlying code of ujson has been written in C, with Python bindings that hook up with the Python interface. The areas that wanted enchancment in the usual JSON library had been optimized in Ujson for velocity and efficiency. However, Ujson is not utilized in newer initiatives, because the makers themselves have talked about on PyPI that the library has been positioned in maintenance-only mode. Under is an illustration of ujson’s processes at a high-level.
import ujson
taxonomy_data = '{"id":1, "genus":"Thylacinus", "species":"cynocephalus", "extinct": true}'
data_dict = ujson.masses(taxonomy_data) #Deserialize
with open("taxonomy_data.json", "w") as fh: #Serialize
ujson.dump(data_dict, fh)
with open("taxonomy_data.json", "r") as fh: #Deserialize
information = ujson.load(fh)
print(information)
We transfer to the subsequent potential library named ‘orjson’.
orjson
Since Orjson is written in Rust, it’s optimized not just for velocity but in addition has memory-safe mechanisms to stop buffer overflows that builders face whereas utilizing C-based JSON libraries like ujson. Furthermore, Orjson helps serialization of a number of further datatypes past the usual Python datatypes, together with dataclass and datetime objects. One other key distinction between orjson and the opposite libraries is that orjson’s dumps() perform returns a bytes object, whereas the others return a string. Returning the information as a bytes object is without doubt one of the predominant causes for orjson’s quick throughput.
import orjson
book_payload = '{"id":1,"title":"The Nice Gatsby","writer":"F. Scott Fitzgerald","Publishing Home":"Charles Scribner's Sons"}'
data_dict = orjson.masses(book_payload) #Deserialize
print(data_dict)
with open("book_data.json", "wb") as f: #Serialize
f.write(orjson.dumps(data_dict)) #Returns bytes object
with open("book_data.json", "rb") as f:#Deserialize
book_data = orjson.masses(f.learn())
print(book_data)
Now that we’ve explored some JSON parsing libraries, let’s take a look at their serialization capabilities.
Testing Serialization Capabilities of JSON, ujson and orjson
We create a pattern dataclass object with an integer, string and a datetime variable.
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Consumer:
id: int
title: str
created: datetime
u = Consumer(id=1, title="Thomas", created=datetime.now())
We then go it to every of the libraries to see what occurs. We start with the stdlib JSON.
import json
attempt:
print("json:", json.dumps(u))
besides TypeError as e:
print("json error:", e)
As anticipated, we get the next error. (The usual JSON library doesn’t assist serialization of “dataclass” objects and datetime objects.)

Subsequent, we take a look at the identical with the ujson library.
import ujson
attempt:
print("json:", ujson.dumps(u))
besides TypeError as e:
print("json error:", e)

As we see above, ujson just isn’t in a position to serialize the information class object and the datetime datatype. Lastly, we use the orjson library for serialization.
import orjson
attempt:
print("orjson:", orjson.dumps(u))
besides TypeError as e:
print("orjson error:", e)
We see that orjson was in a position to serialize each the dataclass and the datetime datatypes.

Working with NDJSON (A particular Point out)
We’ve seen the libraries for JSON parsing, however what about NDJSON? NDJSON (Newline Delimited JSON), as you may know, is a format during which every line is a JSON object. In different phrases, the delimiter just isn’t a comma however a newline character. For example, that is what NDJSON appears like.
{"id": "A13434", "title": "Ella"}
{"id": "A13455", "title": "Charmont"}
{"id": "B32434", "title": "Areida"}
NDJSON is usually used for logs and streaming information, and therefore, NDJSON payloads are wonderful candidates for being parsed utilizing the ijson library. For small to reasonable NDJSON payloads, it is strongly recommended to make use of the stdlib JSON. Apart from ijson and stdlib JSON, there’s a devoted NDJSON library. Under are code snippets exhibiting every method.
NDJSON utilizing stdlib JSON & ijson
As NDJSON just isn’t delimited by commas, it doesn’t qualify for a bulk load, as a result of stdlib json expects to see an inventory of dicts. In different phrases, stdlib JSON’s parser appears for a single legitimate JSON aspect, however is as an alternative given a number of JSON parts within the payload file. Due to this fact, the file must be parsed iteratively, line by line, and despatched to the caller perform for additional processing.
import json
ndjson_payload = """{"id": "A13434", "title": "Ella"}
{"id": "A13455", "title": "Charmont"}
{"id": "B32434", "title": "Areida"}"""
#Writing NDJSON file
with open("json_lib.ndjson", "w", encoding="utf-8") as fh:
for line in ndjson_payload.splitlines(): #Cut up string into JSON obj
fh.write(line.strip() + "n") #Write every JSON object as its line
#Studying NDJSON file utilizing json.masses
with open("json_lib.ndjson", "r", encoding="utf-8") as fh:
for line in fh:
if line.strip(): #Take away new strains
merchandise= json.masses(line) #Deserialize
print(merchandise) #or ship it to the caller perform
With ijson, the parsing is completed as proven under. With customary JSON, we have now only one root aspect, which is both a dictionary if it’s a single JSON or an array if it’s a listing of dicts. However with NDJSON, every line is its personal root aspect. The argument “” in ijson.gadgets() tells the ijson parser to take a look at every root aspect. The arguments “” and multiple_values=True let the ijson parser know that there are a number of JSON root parts within the file, and to fetch one line (every JSON) at a time.
import ijson
ndjson_payload = """{"id": "A13434", "title": "Ella"}
{"id": "A13455", "title": "Charmont"}
{"id": "B32434", "title": "Areida"}"""
#Writing the payload to a file to be processed by ijson
with open("ijson_lib.ndjson", "w", encoding="utf-8") as fh:
fh.write(ndjson_payload)
with open("ijson_lib.ndjson", "r", encoding="utf-8") as fh:
for merchandise in ijson.gadgets(fh, "", multiple_values=True):
print(merchandise)
Lastly, we have now the devoted library NDJSON. It principally converts the NDJSON format to straightforward JSON.
import ndjson
ndjson_payload = """{"id": "A13434", "title": "Ella"}
{"id": "A13455", "title": "Charmont"}
{"id": "B32434", "title": "Areida"}"""
#writing the payload to a file to be processed by ijson
with open("ndjson_lib.ndjson", "w", encoding="utf-8") as fh:
fh.write(ndjson_payload)
with open("ndjson_lib.ndjson", "r", encoding="utf-8") as fh:
ndjson_data = ndjson.load(fh) #returns an inventory of dicts
As you’ve got seen, NDJSON file codecs can normally be parsed utilizing stdlib json and ijson. For very massive payloads, ijson is your best option as it’s memory-efficient. However in case you are seeking to generate NDJSON payloads from different Python objects, the NDJSON library is the best selection. It is because the perform ndjson.dumps() mechanically converts python objects to NDJSON format with out having to iterate over these information buildings.
Now that we’ve explored NDJSON, let’s pivot again to benchmarking the libraries stdlib json, ujson, and orjson.
The rationale IJSON just isn’t thought of for Benchmarking
‘ijson’ being a streaming parser could be very totally different from the majority parsers that we checked out. If we benchmarked ijson together with these bulk parsers, we’d be evaluating apples to oranges. Even when we benchmarked ijson alongside the opposite parsers, we’d get the misunderstanding that ijson is the slowest, when in truth it serves a unique objective altogether. ijson is optimized for reminiscence effectivity and subsequently has decrease throughput than bulk parsers.
Producing a Artificial JSON Payload for Benchmarking Functions
We generate a big artificial JSON payload having 1 million information, utilizing the library ‘mimesis’. This information can be used to benchmark the libraries. The under code can be utilized to create the payload for this benchmarking, should you want to replicate this. The generated file can be between 100 MB and 150 MB in dimension, which I consider, is massive sufficient to conduct assessments on benchmarking.
from mimesis import Individual, Deal with
import json
person_name = Individual("en")
complete_address = Deal with("en")
#streaming to a file
with open("large_payload.json", "w") as fh:
fh.write("[") #JSON array
for i in range(1_000_000):
payload = {
"id": person_name.identifier(),
"name": person_name.full_name(),
"email": person_name.email(),
"address": {
"street": complete_address.street_name(),
"city": complete_address.city(),
"postal_code": complete_address.postal_code()
}
}
json.dump(payload, fh)
if i < 999_999: #To prevent a comma at the last entry
fh.write(",")
fh.write("]") #finish JSON array
Under is a pattern of what the generated information would appear like. As you may see, the handle fields are nested to make sure that the JSON is not only massive in dimension but in addition represents real-world hierarchical JSONs.
[
{
"id": "8177",
"name": "Willia Hays",
"email": "[email protected]",
"handle": {
"road": "Emerald Cove",
"metropolis": "Crown Level",
"postal_code": "58293"
}
},
{
"id": "5931",
"title": "Quinn Greer",
"e mail": "[email protected]",
"handle": {
"road": "Ohlone",
"metropolis": "Bridgeport",
"postal_code": "92982"
}
}
]
Let’s begin with benchmarking.
Benchmarking Pre-requisites
We use the learn() perform to retailer the JSON file as a string. We then use the hundreds() perform in every of the libraries (json, ujson, and orjson) to deserialize the JSON string right into a Python object. To start with, we create the payload_str object from the uncooked JSON textual content.
with open("large_payload1.json", "r") as fh:
payload_str = fh.learn() #uncooked JSON textual content
We then create a benchmarking perform with two arguments. The primary argument is the perform that’s being examined. On this case, it’s the masses() perform. The second argument is the payload_str constructed from the file above.
def benchmark_load(func, payload_str):
begin = time.perf_counter()
for _ in vary(3):
func(payload_str)
finish = time.perf_counter()
return finish - begin
We use the above perform to check for each serialization and deserialization speeds.
Benchmarking Deserialization Pace
We load the three libraries being examined. We then run the perform benchmark_load() in opposition to the hundreds() perform of every of those libraries.
import json, ujson, orjson, time
outcomes = {
"json.masses": benchmark_load(json.masses, payload_str),
"ujson.masses": benchmark_load(ujson.masses, payload_str),
"orjson.masses": benchmark_load(orjson.masses, payload_str),
}
for lib, t in outcomes.gadgets():
print(f"{lib}: {t:.4f} seconds")
As we are able to see, orjson has taken the least period of time for deserialization.

Benchmarking Serialization Pace
Subsequent, we take a look at the serialization velocity of those libraries.
import json
import ujson
import orjson
import time
outcomes = {
"json.dumps": benchmark("json", json.dumps, payload_str),
"ujson.dumps": benchmark("ujson", ujson.dumps, payload_str),
"orjson.dumps": benchmark("orjson", orjson.dumps, payload_str),
}
for lib, t in outcomes.gadgets():
print(f"{lib}: {t:.4f} seconds")
On evaluating run instances, we see that orjson takes the least period of time to serialize Python objects to a JSON object.

Selecting the Finest JSON library in your Workflow

Clipboard & Workflow Hacks for JSON
Let’s suppose that you just’d prefer to view your JSON in a textual content editor akin to Notepad++ or share a snippet (from a big payload) on Slack with a teammate. You’ll rapidly run into clipboard or textual content editor/IDE crashes. In such conditions, one might use Pyperclip or Tkinter. Pyperclip works nicely for payloads inside 50 MB, whereas Tkinter works nicely for medium-sized payloads. For giant payloads, you could possibly write the JSON to a file to view the information.
Conclusion
JSON can appear easy, however the bigger the payload and the extra nesting, the extra these payloads can rapidly flip right into a efficiency bottleneck. This text aimed to focus on how every Python parsing library addresses this problem. Whereas deciding on JSON parsing libraries, velocity and throughput usually are not at all times the primary standards. It’s the workflow that determines whether or not throughput, reminiscence effectivity, or long-term scalability is required for parsing payloads. Briefly, JSON parsing shouldn’t be a one-size-fits-all method.















