Skip to content

Python load() function

  • by

Python load() function is used to read JSON data from files and String. Python built-in module JSON provides the following two methods to decode JSON data.

json.load(file_object)

The json.load() method returns data in the form of a Python dictionary.

Python load() function example

A simple example code read JSON data from a file and converts it into a dictionary. 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("data.json", "r") as read_file:
    res = json.load(read_file)

    for key, value in res.items():
        print(key, ":", value)

Output:

Python load() function

Mapping between JSON and Python entities while decoding

Python JSON conversion table:

JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

Do comment if you have any doubts or suggestions on this Python function 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