Read and Parse JSON file using Python

JSON (JavaScript Object Notation) is a lightweight data format that is easy for humans to read and write and easy for machines to parse and generate. In Python, the built-in json module provides methods for reading and parsing JSON data.

Following is an example of how to read and parse a JSON file in Python:

Sample JSON File

Suppose you have a data.json file containing the following JSON data:

{ "name": "Mark", "age": 40, "city": "London" }

Read and Parse JSON Files

You can read and parse this file using the following Python code:

import json # Open the file with open('data.json') as f: # Load the JSON data data = json.load(f) # Access the data print(data['name']) print(data['age']) print(data['city'])
# Output: Mark 40 London

In the above code, first import the json module. Then use the open() function to open the data.json file and the json.load() function to load the JSON data from the file into a Python Dictionary called data. Then access the data in the dictionary using keys, just like with any other Python dictionary.

Reading JSON from a File with Python json.loads()

If the JSON data is in a string instead of a file, you can use the json.loads() function instead of json.load().

import json # JSON data as a string json_str = '{"name": "Mark", "age": 40, "city": "London"}' # Parse the JSON data data = json.loads(json_str) # Access the data print(data['name']) print(data['age']) print(data['city'])
# Output: Mark 40 London

In the above code, define the JSON data as a string instead of reading it from a file. Use the json.loads() function to parse the JSON data from the string into a Python Dictionary called data. You can then access the data in the dictionary using keys, just like before.

Use json.load() to parse nested objects or arrays

If the JSON data contains nested objects or arrays, you can access the data using indexing or looping, just like with any other Python data structure.

Suppose you have a data.json file containing the following JSON data:

{ "name": "Mark", "age": 40, "city": "London", "pets": [ { "name": "Remo", "species": "cat" }, { "name": "Ramo", "species": "dog" } ] }

You can read and parse this file using the following Python code:

import json # Open the file with open('data.json') as f: # Load the JSON data data = json.load(f) # Access the nested data print(data['name']) print(data['pets'][0]['name']) print(data['pets'][1]['species'])
# Output: Mark Remo dog

In this code, first import the json module and use the open() function to open the data.json file. Then use the json.load() function to load the JSON data from the file into a Python dictionary called data. You can access the nested data in the dictionary using indexing or looping, just like with any other Python data structure. For example, you access the name of the first pet using data['pets'][0]['name'].

Python dictionary and JSON

Python dictionaries and JSON (JavaScript Object Notation) are closely related data structures.


How to work with the Python built-in  json module.
JSON is a lightweight data format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript programming language, but can be used in any programming language, including Python. Python dictionaries are a built-in data structure that allow you to store key-value pairs. Each key in a dictionary must be unique and immutable, while values can be of any data type, including other dictionaries, lists, and tuples.

Convert the Python Dictionary to JSON

JSON data is often used to represent complex data structures, such as nested dictionaries and lists. In fact, JSON data is almost identical to a Python dictionary or list.

import json # A Python dictionary data = { "name": "Mark", "age": 40, "city": "London", "pets": [ { "name": "Remo", "species": "cat" }, { "name": "Ramo", "species": "dog" } ] } # Convert the dictionary to JSON json_str = json.dumps(data) # Print the JSON data print(json.dumps(data, indent = 4, sort_keys=True))

In this code, define a Python Dictionary called data that contains some key-value pairs, including a list of dictionaries. Then use the json.dumps() function to convert the dictionary to a JSON-formatted string.

Print JSON data using Python

You can print the JSON data using print().

print(json.dumps(data, indent = 4, sort_keys=True))

The output should look like this:

{ "name": "Mark", "age": 40, "city": "London", "pets": [ { "name": "Remo", "species": "cat" }, { "name": "Ramo", "species": "dog" } ] }

JSON to a Python Dictionary

You can also convert JSON data back to a Python dictionary using the json.loads() function.

import json # JSON data as a string json_str = '{"name": "Mark", "age": 40, "city": "London"}' # Convert the JSON data to a Python dictionary data = json.loads(json_str) # Access the data print(data['name']) print(data['age']) print(data['city'])
# Output Mark 40 London

In the above code, define a JSON-formatted string called json_str. Then use the json.loads() function to convert the string to a Python dictionary called data. You can then access the data in the dictionary using keys, just like before.