How to convert JSON data into a Python object?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's json module converts JSON strings to Python dictionaries by default, but often you want to work with proper objects that have attribute access (obj.name) instead of dictionary access (obj["name"]). There are several approaches: using json.loads() with an object_hook, types.SimpleNamespace, dataclasses, Pydantic models, or the json.JSONDecoder class. The right choice depends on whether you need type validation, nested objects, or just simple attribute access.
Basic JSON to Dictionary
Method 1: SimpleNamespace with object_hook
types.SimpleNamespace provides attribute-style access with minimal boilerplate.
Method 2: Custom Class with object_hook
Method 3: Dataclasses (Python 3.7+)
Method 4: Pydantic (Recommended for Validation)
Pydantic provides automatic type coercion, validation, and nested model parsing.
Method 5: json.JSONDecoder Subclass
Reading JSON from Files
Common Pitfalls
- Nested objects not converted: A simple
User(**data)only converts the top level. Nested JSON objects remain as dicts. Useobject_hook(which recurses automatically) or manually convert nested structures. - JSON keys that are not valid Python identifiers: JSON keys like
"first-name"or"class"cannot be used as Python attributes directly.SimpleNamespaceallowsgetattr(obj, "first-name")but notobj.first-name. Use__dict__access or rename keys during parsing. - Type mismatches with dataclasses: Dataclasses do not coerce types — passing a string
"30"to anintfield stores the string without error. Use Pydantic if you need automatic type coercion and validation. object_hookcalled for every nested object: Theobject_hookfunction is called bottom-up for every JSON object, including nested ones. If your hook creates a specific class (e.g.,User), nested objects will also be passed to the same hook, causing errors. Check the dict keys inside the hook to determine which class to instantiate.- Losing JSON arrays as Python lists:
object_hookonly transforms JSON objects (curly braces), not JSON arrays (square brackets). Arrays are converted to Python lists with their elements processed individually. This is usually correct but can be surprising if you expect arrays to become custom objects.
Summary
json.loads()returns dicts by default — useobject_hookto convert to objectsSimpleNamespacewithobject_hookis the simplest approach for attribute access on nested JSON- Dataclasses provide structure but require manual nested conversion
- Pydantic provides automatic validation, type coercion, and nested parsing — best for production APIs
object_hookis called recursively for all nested JSON objects automatically
Related reading
- How to convert list of numpy arrays into single numpy array?
- How to convert list to string
- How to convert numpy arrays to standard TensorFlow format?
- How to Convert Ragged Tensor to Tensor in Python?
- How to convert SQL Query result to PANDAS Data Structure?
- How to convert SQLAlchemy row object to a Python dict?
- How to convert string representation of list to a list
- How to convert string representation of list to a list
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.