Python
JSON
data conversion
programming tutorial
serialization

Converting dictionary to JSON

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a Python dictionary to JSON is simple in demos, but production serialization needs deliberate rules for formatting, type conversion, and compatibility. The main tool is the json module, specifically dumps for strings and dump for files. Reliable JSON output also requires handling unsupported Python types and protecting sensitive fields.

Basic Dictionary Serialization

Use json.dumps to convert a dictionary into a JSON string.

python
1import json
2
3payload = {
4    "id": 101,
5    "name": "Alice",
6    "active": True,
7    "roles": ["admin", "editor"]
8}
9
10text = json.dumps(payload)
11print(text)

Key distinction:

  • 'json.dumps returns a string'
  • 'json.dump writes to a file-like object'

Control Readability and Determinism

For debugging and version-controlled artifacts, configure indentation and key order.

python
pretty = json.dumps(payload, indent=2, sort_keys=True)
print(pretty)

Deterministic ordering reduces noisy diffs and helps reproducible builds.

Write JSON Files with UTF-8

When writing files, set encoding and Unicode behavior explicitly.

python
1import json
2
3with open("output.json", "w", encoding="utf-8") as f:
4    json.dump(payload, f, ensure_ascii=False, indent=2)

ensure_ascii=False keeps non-ASCII text readable instead of escape sequences.

Handle Unsupported Types with default

JSON does not natively support objects like datetime, set, or custom classes. Add a conversion function.

python
1import json
2from datetime import datetime
3
4obj = {
5    "created_at": datetime(2026, 3, 5, 12, 0),
6    "tags": {"python", "json"}
7}
8
9def encode_default(value):
10    if isinstance(value, datetime):
11        return value.isoformat()
12    if isinstance(value, set):
13        return sorted(value)
14    raise TypeError(f"Unsupported type: {type(value)}")
15
16text = json.dumps(obj, default=encode_default, indent=2)
17print(text)

Make this logic explicit so conversions stay reviewable.

Dataclasses and Domain Models

For dataclasses, convert to plain dictionaries first.

python
1from dataclasses import dataclass, asdict
2import json
3
4@dataclass
5class User:
6    id: int
7    email: str
8
9u = User(id=1, email="[email protected]")
10print(json.dumps(asdict(u)))

For richer domain objects, create explicit serializer functions instead of relying on implicit internals.

Validate JSON Contracts After Serialization

When data crosses service boundaries, validate required keys and types after a serialize-deserialize round trip.

python
1import json
2
3required = {"id", "name", "active"}
4encoded = json.dumps({"id": 1, "name": "Ana", "active": True})
5decoded = json.loads(encoded)
6
7missing = required - decoded.keys()
8if missing:
9    raise ValueError(f"Missing required keys: {sorted(missing)}")

This catches contract drift early in tests.

Performance Considerations

For high throughput paths:

  • avoid serializing unchanged objects repeatedly
  • serialize near I O boundaries rather than deep in core logic
  • benchmark alternatives only after profiling

The standard library is usually sufficient and has predictable behavior.

Security and Data Hygiene

Serialization code should not leak secrets by default. Avoid dumping full dictionaries from auth or billing contexts directly to logs.

Safer pattern:

  • build an explicit output schema
  • include only approved keys
  • mask sensitive values where needed

Treat JSON serialization as part of security boundary design.

Streaming and Large Payload Handling

When dictionaries become very large, consider streaming JSON generation or chunked response patterns instead of building one huge in-memory string. This reduces peak memory usage and improves resilience in API workers under load. For long-running data exports, include progress logging and output-size guards to prevent accidental oversized payload generation.

Common Pitfalls

A common pitfall is confusing dump and dumps, then wondering why nothing was written to disk.

Another issue is assuming every Python object serializes automatically. Unsupported types should be handled explicitly.

Teams also manually build JSON strings with concatenation, which creates escaping bugs and invalid output.

Summary

  • Use json.dumps for JSON strings and json.dump for file output.
  • Configure formatting and key ordering intentionally.
  • Convert unsupported types via explicit default logic.
  • Validate serialized contracts in tests for boundary reliability.
  • Apply field whitelisting to prevent sensitive data leaks.

Course illustration
Course illustration

All Rights Reserved.