How can I catch new posts?
Bookmark this blog and watch the homepage and tools hub—we surface new guides there. No account or mailing list is required to read articles.
A practical deep dive into Python JSON workflows: custom datetime encoding, readable Unicode output with ensure_ascii=False, streaming GB-scale payloads, and when to choose orjson/ujson.
In Python backends and data pipelines, JSON looks easy until production constraints appear: datetime fields, non-ASCII text, and multi-GB payloads. This guide focuses on the patterns that stay reliable under real traffic.
We will cover:
loads/dumps and load/dumpensure_ascii=Falsejson, orjson, and ujsonloads/dumps vs load/dumpjson.dumps(obj): Python object -> JSON stringjson.loads(text): JSON string -> Python objectjson.dump(obj, file): Python object -> filejson.load(file): file -> Python objectA simple memory aid: s means string.
datetime serialization errorsThe common error:
from datetime import datetime
import json
json.dumps({"now": datetime.now()})
# TypeError: Object of type datetime is not JSON serializableUse a custom encoder and keep the policy centralized:
from datetime import datetime, date
import json
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
if isinstance(obj, date):
return obj.isoformat()
return super().default(obj)
payload = {"now": datetime.now(), "today": date.today()}
print(json.dumps(payload, cls=DateTimeEncoder, ensure_ascii=False, indent=2))Use strftime(...) if your APIs require a fixed textual format.
\uXXXXBy default, json.dumps() escapes non-ASCII characters:
import json
print(json.dumps({"message": "你好,世界"}))
# {"message": "\u4f60\u597d\uff0c\u4e16\u754c"}For readable multilingual output:
import json
print(json.dumps({"message": "你好,世界"}, ensure_ascii=False))When writing files, always pair with UTF-8:
with open("data.json", "w", encoding="utf-8") as f:
json.dump({"message": "你好,世界"}, f, ensure_ascii=False)Loading huge arrays with one json.load() call can blow up memory. Two practical options:
.jsonl)import json
def read_jsonl(path):
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
yield json.loads(line)
for obj in read_jsonl("events.jsonl"):
pass # process incrementallyijson for large array documentspip install ijsonimport ijson
def iter_huge_array(path):
with open(path, "rb") as f:
for item in ijson.items(f, "item"):
yield itemjson vs orjson vs ujsonjson (stdlib): best compatibility, zero dependencyorjson: strong throughput, great for hot pathsujson: easy drop-in in many projects, but verify edge behaviororjson quick example:
import orjson
from datetime import datetime
data = {"name": "Alice", "now": datetime.now()}
raw = orjson.dumps(data) # bytes
obj = orjson.loads(raw)loads/dumps for strings, load/dump for filesensure_ascii=False for multilingual productsAll tools run locally in your browser. Your JSON data is not uploaded.
Dedicated to providing developers with the best JSON processing tools
More posts coming soon...
Back to BlogFollowing the blog, topics we cover, and how to suggest guides.
Bookmark this blog and watch the homepage and tools hub—we surface new guides there. No account or mailing list is required to read articles.
JSON validation, formatting, conversion, debugging workflows, and JSON Work releases—mapped to what the free on-site tools can do locally in your browser.
Yes. Reach out via the About page or GitHub; we prioritize guides tied to real integration and debugging scenarios.