Example JSON file for Python [With Explantion]

What is JSON file in Python?

JSON (JavaScript Object Notation) file is a file that stores data in a structured format.

A JSON file consists of key-value pairs, similar to a Python dictionary.

The keys are strings, and the values can be strings, numbers, booleans, arrays (lists), or nested JSON objects (dictionaries).

JSON files are human-readable and easy to parse in various programming languages, including Python.

Here’s an example of a JSON file:

{
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "address": {
    "street": "123 Main Street",
    "city": "New York",
    "state": "NY",
    "zipcode": "10001"
  },
  "hobbies": ["reading", "playing guitar", "hiking"],
  "is_student": true
}
Code language: Python (python)

In the example above, the JSON file represents a person’s information. It includes fields such as “name”, “age”, “email”, “address”, “hobbies”, and “is_student”.

The “address” field contains another JSON object with the person’s street, city, state, and zipcode.

The “hobbies” field contains an array of strings representing the person’s hobbies, and the “is_student” field is a boolean value indicating whether the person is a student or not.

In Python, you can read, write, and manipulate JSON files using the json module, which provides functions like json.load() and json.dump() for loading and dumping JSON data, respectively.

These functions allow you to convert JSON data into Python data structures and vice versa.

Example json file for python 

Here’s an example of a JSON file in Python:

{
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "address": {
    "street": "123 Main Street",
    "city": "New York",
    "state": "NY",
    "zipcode": "10001"
  },
  "hobbies": ["reading", "playing guitar", "hiking"],
  "is_student": true
}
Code language: Python (python)

In Python, you can work with JSON files using the json module. Here’s an example of how you can read and parse the above JSON file:

import json

# Read JSON file
with open('example.json') as file:
    data = json.load(file)

# Access data from JSON
name = data['name']
age = data['age']
email = data['email']
street = data['address']['street']
city = data['address']['city']
state = data['address']['state']
zipcode = data['address']['zipcode']
hobbies = data['hobbies']
is_student = data['is_student']

# Print the data
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Email: {email}")
print(f"Address: {street}, {city}, {state} {zipcode}")
print("Hobbies:", ', '.join(hobbies))
print(f"Is a student: {is_student}")
Code language: Python (python)

Remember to replace 'example.json' with the actual path to your JSON file.

Explanation of the Code

Let’s go through the code step by step:

1. To start working with JSON data in Python, the json module must first be imported, which offers several functions for this purpose.

import jsonCode language: Python (python)

2. The open function is used to access the JSON file and assign it to the variable ‘file‘. Furthermore, the with statement ensures that once all operations are done, the file will be automatically closed.

with open('example.json') as file:Code language: Python (python)

3. Inside the with block, we use the json.load function to load the contents of the JSON file into a Python data structure. The json.load function takes the file object as input and returns the parsed JSON data.

    data = json.load(file)Code language: Python (python)

4. Once the JSON data is loaded into the data variable, we can access individual elements within the JSON structure. In this example, we extract various pieces of information such as name, age, email, address, hobbies, and is_student.

name = data['name']
age = data['age']
email = data['email']
street = data['address']['street']
city = data['address']['city']
state = data['address']['state']
zipcode = data['address']['zipcode']
hobbies = data['hobbies']
is_student = data['is_student']
Code language: Python (python)

5. Finally, we print the extracted data to the console using print statements. We use formatted strings (f-strings) to display the values of the variables.

print(f"Name: {name}")
print(f"Age: {age}")
print(f"Email: {email}")
print(f"Address: {street}, {city}, {state} {zipcode}")
print("Hobbies:", ', '.join(hobbies))
print(f"Is a student: {is_student}")
Code language: Python (python)

By running this code, you will see the values of the JSON data displayed on the console.

Note: Make sure to replace 'example.json' in the code with the actual path to your JSON file if it’s stored in a different location.

Read More;

  • I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment