JSON is called as Java script object notation. This is the most popular format used for data transfer as text and widely used across multiple databases and API’s. The best part of JSON is, it is human readable and machine generatable as well. When working with JSON files there are two tasks involved they are Serialization and Deserialization and this tutorial covers both of them.
The process of creating JSON objects by converting native python objects into JSON formatted data is called Serialization. This procedure encodes the native python data into JSON format and this can be achieved by two methods they are dump() and dumps() and we need to import JSON to use either of the methods.
Both the method dump and dumps create JSON format data, however, there is a slight difference in the approach of these methods. The dump method basically works with file objects, i.e. writing to a JSON extension files from another file, on the other hand, dumps method converts a python based dictionary to a JSON string.
You can observe, the dump method writes the content of the data to the “sample.json”, you can also find the output of the “sample.json” file when displayed.
You can observe, the dumps method creates a JSON string from the python dictionary object and its type is string.
This is about decoding the JSON based files into python native data. The deserialization can be achieved via two methods that includes load() and loads() methods. The load method is used to read data from JSON files to python objects and loads method converts JSON string to dictionary.
You can observe, the “example_2.json” is loaded into the variable data and it’s type is dict (Dictionary).
Now the json_object (JSON String ) is read a into the variable data and its data type is <class ‘dict’>
Reading complex json files:
Now, we know how to read and write json files. But, in real time scenarios we get complex json files that are very hard to read. Regular approaches doesn’t work, we need to customize the operations of reading or writing as per our requirement. In this tutorial, we learn how to read a complex json file.
Observe how the json file is positioned, it has dictionaries with in dictionaries, creating a loop of dictionaries. Also, it has multiple records, where each record containing certain attributes.
If we read this file using regular methods, it will throw an error.
To read files of such of complexity, we have to our own customize scripts.
We have created an empty dictionary records, that was supposed to hold the entire JSON file. With open statement we are trying to read JSON file with file pointer “f” and assigning each record in the file to records dictionary, that contains the key in incremental order of integer i.
You can observe the format of each line in “complex_json.json” file before conversion to native data type is str and after applying the loads method it is dict. Also, you can view the data format stored in each record.
Here, we are reading the JSON file record by record and assigning it to dictionary element of our chosen dictionary i.e. records.
In this tutorial, we have learned what is JSON data, how to read and write JSON files. Understand the difference between dump and dumps, load and loads and how we use these methods. Also, went through reading a complex JSON format using customized scripts. Hope you enjoyed this episode on reading JSON files. Keep reading. Keep Learning !!!