Read JSON File In Python

JavaScript Object Notation or JSON is a popular data format in order to store objects in JavaScript programming language. JSON data format is very popular the developers in order to store data and information in a simple and human-readable format. Python also supports the JSON and provides different functions in order to read, write, and change JSON data and files. Python dictionary is also used as a native data type in order to store JSON data in an efficient way.

Python json Module

The json module provides required methods, structures to work with JSON data and format. This module basicly provides to read and write or decode or encode methods for JSON format. In order to read a JSON data file or JSON data string this module should be loaded with the import like below.

import json

JSON Object and Python Object Convertion

The JSON uses different object types which is derived from the JavaScript programming language. During deserialization all of these object type should be converted into related Python type. This conversion done automatically during load of the JSON data. Below we will list the JSON object types and related Python Object types.

JSON OBJECTPYTHON OBJECT
objectdict
arraylist
stringstr
nullNone
number (int)int
number (real)float
trueTrue
falseFalse

Read (Deserialize) JSON Data From File

One of the most popular way for JSON is reading the JSON from a file. First in order to read a file it should be open with the open() method and the opened file will be provided into the json.load() method which will read the data and then deserialize into the Python object. JSON data is converted into the Python as dictionary and all other contents are put inside this dictionary as key/value pair.

#Import the Python json module 
import json

#Open the json file which is named file.json
f = open(file.json)

#read and deserialize the json file
# The data will be a dictionary
data = json.load(f)

#Print the json object which is a dictionary type
print(data)

#close the json file
f.close()

Read (Deserialize) JSON Data From String

The json module also provides the json.loads() which will load and deserialize the JSON data which is a string. The json.loads(str) will read the JSON data which is stored with the str and return the deserialized way as a dictionary data type.

#Import the Python json module 
import json

# The p contains the JSON data as string
p = '{"name": "Ali", "languages": ["English", "Turkish"]}'

#read and deserialize the json file
# The data will be a dictionary
data = json.load(str)

#Print the json object which is a dictionary type
print(data)

Pretty Print JSON Data (Python Dictionary)

JSON data can be presented in different ways. By default printing, the JSON data will be not formatted. But in order to increase the readability with indents and sorting keys. The json.load() provides the indent parameter to set indentation space count, sort_keys parameter to enable sorting items according to keys, etc.

#Import the Python json module 
import json

# The p contains the JSON data as string
p = '{"name": "Ali", "languages": ["English", "Turkish"]}'

#read and deserialize the json file
# The data will be a dictionary
data = json.load(p , indent=3 , sort_keys=True)

#Print the json object which is a dictionary type
print(data)

Leave a Comment