UPDATE

With Python3, you can do it in one line, using SimpleNamespace and object_hook:

import json
from types import SimpleNamespace

data = '{"name": "John Smith", "hometown": {"name": "New York", "id": 123}}'

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d))
# Or, in Python 3.13+:
#   json.loads(data, object_hook=SimpleNamespace)
print(x.name, x.hometown.name, x.hometown.id)

OLD ANSWER (Python2)

In Python2, you can do it in one line, using namedtuple and object_hook (but it's very slow with many nested objects):

import json
from collections import namedtuple

data = '{"name": "John Smith", "hometown": {"name": "New York", "id": 123}}'

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
print x.name, x.hometown.name, x.hometown.id

or, to reuse this easily:

def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)

x = json2obj(data)

If you want it to handle keys that aren't good attribute names, check out namedtuple's rename parameter.

Answer from DS. on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
JSON encoder and decoder โ€” Python 3.14.3 documentation
4 weeks ago - Identical to load(), but instead of a file-like object, deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
JSON is text, written with JavaScript object notation. Python has a built-in package called json, which can be used to work with JSON data. ... If you have a JSON string, you can parse it by using the json.loads() method.
Discussions

How to convert JSON data into a Python object? - Stack Overflow
I have written a small (de)serialization framework called any2any that helps doing complex transformations between two Python types. In your case, I guess you want to transform from a dictionary (obtained with json.loads) to an complex object response.education ; response.name, with a nested ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How should/can I convert loaded JSON data into Python objects?
I have JSON input like this: { ... a Python object in a way similar to how serde from Rust works. I would like to define a class like this: @dataclass class MyClass: accountID: str accountClass: str id: str openTime: str priceDifference: float After loading the JSON data what ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
1
October 25, 2022
How to turn JSON into objects in python?
It's easier (and probably better) if you define the class first. Then, turn the json into a dict, then into an instance of that class. import json class Person: def __init__(self, name, job): self.name = name self.job = job person_json = '''{ "name":"Harry", "job":"Mechanic" }''' person_dict = json.loads(person_json) person = Person(**person_dict) Edit: Sorry I didn't answer your exact question. But maybe that was still helpful. There's also attrdict . Making a whole class from a dict is certainly possible, but not something most people would want to do. And when you say "yield", do you mean you want the Person definition as Python code, or the Person class object? More on reddit.com
๐ŸŒ r/learnpython
8
2
September 2, 2021
python - Loading and parsing a JSON file with multiple JSON objects - Stack Overflow
I am trying to load and parse a JSON file in Python. But I'm stuck trying to load the file: import json json_data = open('file') data = json.load(json_data) Yields: ValueError: Extra data: line 2 More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
August 20, 2025 - By using json.loads(), you can convert JSON data back into Python objects.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-load-in-python
json.load() in Python - GeeksforGeeks
August 11, 2025 - Values can be different JSON data types such as strings, numbers, booleans, arrays, or other JSON objects. json.load() function in Python is used to read a JSON file and convert it into a corresponding Python object, such as a dictionary or a list.
Top answer
1 of 16
735

UPDATE

With Python3, you can do it in one line, using SimpleNamespace and object_hook:

import json
from types import SimpleNamespace

data = '{"name": "John Smith", "hometown": {"name": "New York", "id": 123}}'

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d))
# Or, in Python 3.13+:
#   json.loads(data, object_hook=SimpleNamespace)
print(x.name, x.hometown.name, x.hometown.id)

OLD ANSWER (Python2)

In Python2, you can do it in one line, using namedtuple and object_hook (but it's very slow with many nested objects):

import json
from collections import namedtuple

data = '{"name": "John Smith", "hometown": {"name": "New York", "id": 123}}'

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
print x.name, x.hometown.name, x.hometown.id

or, to reuse this easily:

def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)

x = json2obj(data)

If you want it to handle keys that aren't good attribute names, check out namedtuple's rename parameter.

2 of 16
206

You could try this:

class User():
    def __init__(self, name, username):
        self.name = name
        self.username = username

import json
j = json.loads(your_json)
u = User(**j)

Just create a new object and pass the parameters as a map.


You can have a JSON with objects too:

import json
class Address():
    def __init__(self, street, number):
        self.street = street
        self.number = number

    def __str__(self):
        return "{0} {1}".format(self.street, self.number)

class User():
    def __init__(self, name, address):
        self.name = name
        self.address = Address(**address)

    def __str__(self):
        return "{0} ,{1}".format(self.name, self.address)

if __name__ == '__main__':
    js = '''{"name":"Cristian", "address":{"street":"Sesame","number":122}}'''
    j = json.loads(js)
    print(j)
    u = User(**j)
    print(u)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-loads-in-python
json.loads() in Python - GeeksforGeeks
Explanation: json.loads(s) parses the JSON string s and converts it into a Python dict. ... Return Type: Returns a Python object such as dict, list, int, float, or str depending on the JSON content.
Published ย  January 13, 2026
๐ŸŒ
Imperial College London
python.pages.doc.ic.ac.uk โ€บ java โ€บ lessons โ€บ java โ€บ 10-files โ€บ 03-load.html
Python for Java Programmers > Loading JSON files
To load your data from a JSON file, use json.load(file_object). The following code loads the content from a file called input.json into a Python object named data.
Find elsewhere
๐ŸŒ
Zyte
zyte.com โ€บ home โ€บ blog โ€บ json parsing with python [practical guide]
JSON Parsing with Python [Practical Guide]
July 6, 2023 - The json module provides two methods, loads and load, that allow you to parse JSON strings and JSON files, respectively, to convert JSON into Python objects such as lists and dictionaries.
๐ŸŒ
Medium
medium.com โ€บ @gadallah.hatem โ€บ the-difference-between-json-loads-and-json-load-2dbd30065f26
The difference between json.loads() and json.load() in Python | by Hatem A. Gad | Medium
December 15, 2024 - Input: A file object or any object supporting .read() method (e.g., opened file). Use Case: When you are working with JSON data stored in a file. import json # JSON file (e.g., data.json containing {"name": "Alice", "age": 30, "is_member": true}) with open('data.json', 'r') as file: # Convert JSON data in the file to Python dictionary data = json.load(file) print(data) # Output: {'name': 'Alice', 'age': 30, 'is_member': True}
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ json module โ€บ .load()
Python | JSON Module | .load() | Codecademy
May 29, 2025 - Returns a Python object (dictionary, list, string, number, boolean, or None) representing the parsed JSON data. This example demonstrates the fundamental usage of json.load() to read JSON data from a file:
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ json_load_function.htm
Python json.load() Function
The Python json.load() function is used to read JSON data from a file and convert it into a corresponding Python object. This function is useful when dealing with data stored in JSON format, such as configuration files, API responses, or structured
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ read-json-file-using-python
Read JSON file using Python - GeeksforGeeks
... We will be using Pythonโ€™s json module, which offers several methods to work with JSON data. In particular, loads() and load() are used to read JSON from strings and files, respectively.
Published ย  September 15, 2025
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How should/can I convert loaded JSON data into Python objects? - Python Help - Discussions on Python.org
October 25, 2022 - I have JSON input like this: { ... a Python object in a way similar to how serde from Rust works. I would like to define a class like this: @dataclass class MyClass: accountID: str accountClass: str id: str openTime: str priceDifference: float After loading the JSON data what ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ loading-a-json-file-in-python-how-to-read-and-parse-json
Loading a JSON File in Python โ€“ How to Read and Parse JSON
July 25, 2022 - For managing JSON files, Python has the json module. This module comes with many methods. One of which is the loads() method for parsing JSON strings.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ json โ€บ python json parsing using json.load() and loads()
Python JSON Parsing using json.load() and loads()
May 14, 2021 - As we already discussed in the article, a object_pairs_hook parameter of a json.load() method is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs.
๐ŸŒ
Leapcell
leapcell.io โ€บ blog โ€บ how-to-read-json-in-python
How to Read JSON in Python | Leapcell
July 25, 2025 - Use json.loads() to parse JSON strings into Python objects.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python json.loads() method with examples
Python json.loads() Method with Examples - Spark By {Examples}
May 31, 2024 - The method takes a JSON string as an input param and returns a Python object, usually a dictionary or a list, depending on the structure of the JSON string. ... Note that the json.loads() method can raise a ValueError if the input string is ...