The json.load() method (without "s" in "load") can read a file directly:

import json

with open('strings.json') as f:
    d = json.load(f)
    print(d)

You were using the json.loads() method, which is used for string arguments only.


The error you get with json.loads is a totally different problem. In that case, there is some invalid JSON content in that file. For that, I would recommend running the file through a JSON validator.

There are also solutions for fixing JSON like for example How do I automatically fix an invalid JSON string?.

Answer from ubomb on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
August 20, 2025 - You can use the json.load() function to deserialize JSON data from a file into a Python object.
๐ŸŒ
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
import json # some JSON: x = '{ ... ยท If you have a Python object, you can convert it into a JSON string by using the json.dumps() method....
๐ŸŒ
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 - Also notice how null from the JSON is converted to None in python. This is because null is not valid in Python. The json module also has the load method which you can use to read a file object and parse it at the same time.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ read-json-file-using-python
Read JSON file using Python - GeeksforGeeks
... import json with open('data.json', 'r') as file: data = json.load(file) print(json.dumps(data, indent=4)) ... In this example, we are reading data from the "data.json" file, and the output retains the same structured format as the original ...
Published ย  September 15, 2025
๐ŸŒ
Imperial College London
python.pages.doc.ic.ac.uk โ€บ java โ€บ lessons โ€บ java โ€บ 10-files โ€บ 03-load.html
Python for Java Programmers > Loading JSON files
The json module allows you to easily load a JSON file into its equivalent Python object (usually a dict or list). To load your data from a JSON file, use json.load(file_object).
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Zyte
zyte.com โ€บ home โ€บ blog โ€บ json parsing with python [practical guide]
JSON Parsing with Python [Practical Guide]
July 6, 2023 - Python provides several built-in libraries for reading JSON from files, APIs, and web applications. To read JSON data, you can use the built-in json module (JSON Encoder and Decoder) in Python. 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.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-read-json-file-in-python
How to read JSON file in Python
Suppose we have json file named "persons.json" with contents as shown in Example 2 above. We want to open and read it using python. This can be done in following steps ? ... Read the json file using load() and put the json data into a variable. Use the data retrieved from the file or simply ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-read-json-file-how-to-load-json-from-a-file-and-parse-dumps
Python Read JSON File โ€“ How to Load JSON from a File and Parse Dumps
October 27, 2020 - This is a variable that we can use within the with statement to refer to the file object. ... json.load(file) creates and returns a new Python dictionary with the key-value pairs in the JSON file.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ json module โ€บ .load()
Python | JSON Module | .load() | Codecademy
May 29, 2025 - The .load() method in Pythonโ€™s JSON module is used to parse JSON data from a file-like object and convert it into a Python object. This method reads JSON content directly from files, such as .json files, and transforms the structured data ...
๐ŸŒ
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
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-read-json-file
Python Read JSON File
March 7, 2020 - ... import json fileObject = open("data.json", "r") jsonContent = fileObject.read() aList = json.loads(jsonContent) print(aList[0]) print(aList[0]['c']) ... In this Python JSON Tutorial, we learned how to read JSON file in Python and access values from the JSON content.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python
April 18, 2023 - Any file-like object can be passed to the second argument of the dump() function, even if it isn't an actual file. A good example of this would be a socket, which can be opened, closed, and written to much like a file. The mapping between dictionary contents and a JSON string is straightforward, ...
๐ŸŒ
DEV Community
dev.to โ€บ pineapple_26 โ€บ python-load-json-from-file-14i2
Python Load Json From File - DEV Community
November 20, 2025 - The json.load() function reads JSON data directly from a file object and converts it to a Python dictionary or 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)