• 0

Json loads vs Json Dumps

The json module in Python is widely used for handling JSON data. JSON (JavaScript Object Notation) is a lightweight data format that is easy to read and write for humans and machines. The json module provides four main methods to work with JSON: load, loads, dump, and dumps. Here's a detailed explanation of each method with examples.

json.load vs json.loads

json.load

- Reads JSON data from a file-like object and converts it into a Python object.

- Commonly used when the JSON data is stored in a file.


import json

# JSON data stored in a file
with open('data.json', 'r') as file:
    data = json.load(file)  # Converts JSON to Python object
    print(data)
    

json.loads

- Reads JSON data from a string and converts it into a Python object.

- Useful when the JSON data is received from a web API or other sources as a string.


import json

# JSON data in string format
json_string = '{"name": "Alice", "age": 25, "city": "New York"}'
data = json.loads(json_string)  # Converts JSON string to Python object
print(data)
    

json.dump vs json.dumps

json.dump

- Writes a Python object as JSON into a file-like object.

- Commonly used to save JSON data into a file.


import json

# Python dictionary
data = {"name": "Alice", "age": 25, "city": "New York"}

# Write JSON data to a file
with open('output.json', 'w') as file:
    json.dump(data, file, indent=4)  # Converts Python object to JSON and writes to file
    

json.dumps

- Converts a Python object into a JSON string.

- Commonly used to prepare JSON data for transmission or display.


import json

# Python dictionary
data = {"name": "Alice", "age": 25, "city": "New York"}

# Convert Python object to JSON string
json_string = json.dumps(data, indent=4)
print(json_string)
    

Key Differences Between the Methods

Method Input Output Common Use Case
json.load File-like object Python object Reading JSON from a file
json.loads JSON string Python object Parsing JSON data received as a string
json.dump Python object JSON written to a file-like object Writing JSON data to a file
json.dumps Python object JSON string Converting Python object to JSON string

Examples: All Methods Together


import json

# 1. JSON string
json_string = '{"name": "Alice", "age": 25, "city": "New York"}'

# 2. Parsing JSON string to Python object
data = json.loads(json_string)
print("From string to Python object:", data)

# 3. Saving Python object to a JSON file
with open('output.json', 'w') as file:
    json.dump(data, file, indent=4)

# 4. Reading JSON file back into a Python object
with open('output.json', 'r') as file:
    data_from_file = json.load(file)
print("From file to Python object:", data_from_file)

# 5. Converting Python object to JSON string
json_output_string = json.dumps(data_from_file, indent=4)
print("From Python object to JSON string:\n", json_output_string)
    

Tips to Remember the Difference

  • Load = JSON → Python
    • load: Reads from a file.
    • loads: Reads from a string.
  • Dump = Python → JSON
    • dump: Writes to a file.
    • dumps: Writes to a string.

Conclusion

The json module provides flexible methods for converting between JSON and Python objects. Use load/loads when converting JSON into Python, and dump/dumps when converting Python into JSON. Choose the specific method based on whether your data resides in a file or a string.