JavaScript Object Notation or JSON is a popular data format in order to store objects in the 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 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 the required methods, and structures to work with JSON data and format. This module basically provides 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 Conversion
The JSON uses different object types which are derived from the JavaScript programming language. During deserialization, all of these object types should be converted into related Python types. This conversion was done automatically during the load of the JSON data. Below we will list the JSON object types and related Python Object types.
JSON OBJECT | PYTHON OBJECT |
---|---|
object | dict |
array | list |
string | str |
null | None |
number (int) | int |
number (real) | float |
true | True |
false | False |
Read (Deserialize) JSON Data From File
One of the most popular ways 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 in the json.load() method which will read the data and then deserialize it into the Python object. JSON data is converted into Python as a 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.loads(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
str = '{"name": "Ali", "languages": ["English", "Turkish"]}'
#read and deserialize the json file
# The data will be a dictionary
data = json.loads(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.dumps() 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
d = json.loads(p)
#Print the json object which is a dictionary type
print(json.dumps(d, indent=3 , sort_keys=True))
Convert Python Objects Into JSON String
Below we provide examples to convert all compatible Python objects into JSON strings. In the following example, we convert dictionary, list, tuple, string, integer, floating-point, boolean, and none into JSON.
import json
#Dictionary to JSON
print(json.dumps({"name": "Ahmet", "age": 9}))
#Convert List to JSON
print(json.dumps(["Ankara", "Istanbul"]))
#Convert Tuple to JSON
print(json.dumps(("Ankara", "Istanbul")))
#Convert String to JSON
print(json.dumps("Selam"))
#Convert Integer to JSON
print(json.dumps(12))
#Convert Floating-point to JSON
print(json.dumps(12.34))
#Convert Boolean to JSON
print(json.dumps(True))
#Convert Boolean to JSON
print(json.dumps(False))
#Convert None to JSON
print(json.dumps(None))