If you want to iterate over both keys and values of the dictionary, do this:

for key, value in data.items():
    print(key, value)
Answer from Lior on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
You can convert Python objects of the following types, into JSON strings:
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
August 20, 2025 - After importing the json module, you can use .dumps() to convert a Python dictionary to a JSON-formatted string, which represents a JSON object. Itโ€™s important to understand that when you use .dumps(), you get a Python string in return.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
JSON encoder and decoder โ€” Python 3.14.3 documentation
JSONDecodeError โ€“ When the data being deserialized is not a valid JSON document. UnicodeDecodeError โ€“ When the data being deserialized does not contain UTF-8, UTF-16 or UTF-32 encoded data. ... Added the optional object_pairs_hook parameter. parse_constant doesnโ€™t get called on โ€˜nullโ€™, ...
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)
๐ŸŒ
Zyte
zyte.com โ€บ home โ€บ blog โ€บ json parsing with python [practical guide]
JSON Parsing with Python [Practical Guide]
July 6, 2023 - However, it is essential to remember ... When loading JSON data into Python, it is converted into a Python object, typically a dictionary or list, and can be manipulated using the standard methods of Python objects...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ json
Python JSON: Read, Write, Parse JSON (With Examples)
Suppose, you have a file named person.json which contains a JSON object. {"name": "Bob", "languages": ["English", "French"] } ... import json with open('path_to_file/person.json', 'r') as f: data = json.load(f) # Output: {'name': 'Bob', 'languages': ['English', 'French']} print(data) Here, we have used the open() function to read the json file. Then, the file is parsed using json.load() method which gives us a dictionary named data. If you do not know how to read and write files in Python, we recommend you to check Python File I/O.
Find elsewhere
๐ŸŒ
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: { "accountID": "001-002-003-004", "accountClass": "Primary", "id": "1-2", "openTime": "2019-12-21T02:12:17.122Z", "priceDifference": "0.12345", } I would like to deserialise it inโ€ฆ
๐ŸŒ
Oxylabs
oxylabs.io โ€บ blog โ€บ python-parse-json
Reading & Parsing JSON Data With Python: Tutorial
The load() method takes up a file object and returns the JSON data parsed into a Python object. To get the file object from a file path, Pythonโ€™s open() function can be used.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ g4nr6w3u โ€บ python-parse-json-example
How to parse a JSON with Python?
Nested JSON objects will also be processed and included in the dictionary (see example below). To parse a JSON file, use the json.load() paired method (without the "s"). In this Python Parse JSON example, we convert a JSON data string into a ...
๐ŸŒ
Python Basics
pythonbasics.org โ€บ json
Working With JSON Data in Python - Python Tutorial
You can parse a JSON object with python. The object will then be converted to a python object. ... You can get JSON objects directly from the web and convert them to python objects.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-json
Python JSON - GeeksforGeeks
December 23, 2025 - This is JSON <class 'str'> Now convert from JSON to Python Converted to Python <class 'dict'> {'id': '09', 'name': 'Nitin', 'department': 'Finance'} Let's see an example where we convert Python objects to JSON objects.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-use-the-json-module-in-python
How to Use the JSON Module in Python โ€“ A Beginner's Guide
June 5, 2023 - To create a Python dictionary from JSON data, you can use the json.loads() function provided by the JSON module. This function takes a JSON string and converts it into a corresponding Python object, typically a dictionary.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ how-to-work-with-json-in-python-aef62d28eac4
How to Work with JSON in Python | Medium
November 2, 2024 - You can fetch JSON data from an API and load it into a Python object using the requests library. Note: The requests library is used in this example. If you don't have it installed, you can install it using the following command: ... import json ...
๐ŸŒ
Medium
medium.com โ€บ data-science โ€บ working-with-json-data-in-python-45e25ff958ce
JSON in Python Tutorial | TDS Archive
August 11, 2021 - With the library called requests you can get data from an API. You then need to convert the data to an python object, this can be easily done just use response.json() without any arguments.
๐ŸŒ
DEV Community
dev.to โ€บ bluepaperbirds โ€บ get-all-keys-and-values-from-json-object-in-python-1b2d
Get all keys and values from json object in Python - DEV Community
January 12, 2021 - Using load function json file, this let me keep it into a variable called data. ... Then you have a Python object. Now you can get the keys and values. The code below depends on what your json file looks like.