How to Work with JSON Data in Python

86 views

How to Work with JSON Data in Python

How to Work with JSON Data in Python

I need to read and modify JSON data in Python. How can I do this?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

The json module allows you to read, write, and manipulate JSON data.

Code Example:

import json

def modify_json(json_file, key, value):
    with open(json_file, 'r') as file:
        data = json.load(file)
    data[key] = value
    with open(json_file, 'w') as file:
        json.dump(data, file, indent=4)

# Example usage
modify_json("example.json", "name", "John Doe")

This function reads a JSON file, updates the value associated with a specified key, and writes the updated data back to the file.

solveurit24@gmail.com Changed status to publish February 16, 2025
0