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 data storage.

Syntax

Following is the syntax of the Python json.load() function −

json.load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)

Parameters

This function accepts the following parameters −

  • fp: A file-like object from which JSON data will be read.
  • cls (optional): A custom decoder class that subclasses json.JSONDecoder.
  • object_hook (optional): A function that transforms dictionary objects.
  • parse_float (optional): A function that parses floating-point numbers. Default is float.
  • parse_int (optional): A function that parses integer numbers. Default is int.
  • parse_constant (optional): A function that parses constant values such as NaN and Infinity.
  • object_pairs_hook (optional): A function that processes key-value pairs in a dictionary.

Return Value

This function returns a Python object that represents the JSON data from the file.

Example: Basic Usage

In this example, we use the json.load() function to read JSON data from a file (data.json) and convert it into a Python dictionary −

JSON File (data.json):

Below is the content of our "data.json" file −

{
    "name": "John",
    "age": 30,
    "city": "New York"
}

Following is our code −

import json

# Open the JSON file for reading
with open('data.json', 'r') as file:
   # Deserialize JSON data into a Python dictionary
   data = json.load(file)

print("Data from file:", data)

Following is the data read in the file −

Data from file: {'name': 'John', 'age': 30, 'city': 'New York'}

Example: Custom Parsing

The object_hook parameter allows us to define a custom function that modifies the JSON objects when they are loaded. In this example, we convert all keys to uppercase. −

import json

# Custom function to transform JSON objects
def custom_decoder(obj):
   return {key.upper(): value for key, value in obj.items()}

# Open the JSON file for reading
with open('data.json', 'r') as file:
   # Deserialize JSON data with custom object_hook
   data = json.load(file, object_hook=custom_decoder)

print("Transformed Data:", data)

We obtain the following converted custom Python objects in our file −

Transformed Data: {'NAME': 'John', 'AGE': 30, 'CITY': 'New York'}

Example: Parsing Numbers

In this example, we use the parse_int and parse_float parameters to customize how numbers are processed while loading JSON data −

JSON File (numbers.json):

Below is the content of our "numbers.json" file −

{
    "integer": "10",
    "floating": "12.3456"
}

Following is our code −

import json

# Custom parsing functions
def parse_int_custom(value):
   return int(value) * 10  # Multiply integers by 10

def parse_float_custom(value):
   return round(float(value), 2)  # Round floats to 2 decimal places

# Open the JSON file for reading
with open('numbers.json', 'r') as file:
   # Deserialize JSON data with custom number parsing
   data = json.load(file, parse_int=parse_int_custom, parse_float=parse_float_custom)

print("Parsed Data:", data)

Following customization is reflect in our file −

Parsed Data: {'integer': '10', 'floating': '12.3456'}

Example: Preserving Key Order

By default, dictionaries in Python do not maintain order before Python 3.7. If you need to preserve the order of keys while reading JSON, you can use the object_pairs_hook parameter with OrderedDict

import json
from collections import OrderedDict

# Open the JSON file for reading
with open('data.json', 'r') as file:
   # Deserialize JSON data while preserving key order
   data = json.load(file, object_pairs_hook=OrderedDict)

print("Ordered Data:", data)

Following details is obtained in our file −

Ordered Data: OrderedDict({'name': 'John', 'age': 30, 'city': 'New York'})
python_json.htm
Advertisements