Skip to content

Read JSON file Python

  • by

Use json.load() method to Read JSON files in Python. To work with JSON (string, or file containing JSON object), use Python’s json module. You need to import the module before you can use it.

import json
json.load(file object)

The json.load() accepts the file object, parses the JSON data, populates a Python dictionary with the data, and returns it back to you.

Read JSON file Python example

A simple example code opens the file ‘sample.json’ and parses it. 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.

import json

with open('sample.json', 'r') as f:
    data = json.load(f)
    print(data)
    print(type(data))

Output:

Read JSON file Python

You can convert a dictionary to JSON string using json.dumps() method.

res = json.dumps(data_dict)

Python objects and their equivalent conversion to JSON.

PythonJSON Equivalent
dictobject
list, tuplearray
strstring
int, float, intnumber
Truetrue
Falsefalse
Nonenull

Do comment if you have any doubts or suggestions on this Python JSON file topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading