How to Extract Information from a JSON File in Python?

Estimated read time 2 min read

To extract information from a JSON file in Python, you can use the built-in json module. Here’s an example code snippet that reads a JSON file and prints out the values of specific keys:

import json

# Open the JSON file
with open('data.json') as json_file:
    # Load JSON data
    data = json.load(json_file)

    # Extract information from the JSON data
    name = data['name']
    age = data['age']
    hobbies = data['hobbies']

    # Print out the extracted information
    print('Name:', name)
    print('Age:', age)
    for hobby in hobbies:
        print(' -', hobby)

In this example, we assume that the JSON file is located at data.json and contains the following data:

    "name": "John Doe",
    "age": 30,
    "hobbies": [
        "playing guitar",

The code reads the JSON file using the open function and the json.load method to parse the JSON data into a Python object. Then, it extracts specific values from the object by accessing the corresponding keys using square bracket notation. Finally, it prints out the extracted information.

You can customize the code to extract any information you need from the JSON file, based on the structure of the data.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply