JSON Object in python
JSON Object in python
let start for the basic and the well know definition of what is JSON
JSON stand for Java Script Object Notation and it is a light weight data format with many similarities to python dictionaries. JSON objects can be parse by many, if not all, modern browsers, which is ideal for transporting data between a client and a server.
Defining a JSON¶
First let make and example file, that can be copy and save as a .txt and it will represent our JSON file
null
.
A python dictionary equivalent is:
Notice that in python dictionaries boolean values are capitalize and empty value are None
I will save this JSON in a .txt and called 'album.txt', later import json
library and use dir()
to check the available methods:
load()
and dump()
¶
We will focus in load()
and dump()
methods, for that we will open the file, and album.txt that contain a JSON object, we will open this file using open()
and we can choose if open in writing mode or reading mode, this case will be reading so the second argument will be 'r'
if we use print(type(album))
the result will be a python dictionary type
We can verify this by checking two values, the value of ‘won_grammy’ that now is False
and the value of ‘album_sale’ that now is None
.
Not that is parse as a dictionary we can call the different values as a dictionary:
JSON in a string form and the method loads()
¶
In client server applications it is common for JSON objects to arrive in the form of strings. For example, our JSON object for album information can look something like this:
To load this data using the JSON module, we use the loads()
method:
Now, suppose we start with dictionary formatted data and wish to send this data to a database in JSON format.
We can use the dumps()
methods to convert dictionaries to string JSON objects.
We see that the ‘album_sale’ value, ‘None’, is now ‘null’ and the ‘won_grammy’ value, ‘False’, is now ‘false’.
Finally, we can write this JSON object to a ‘.txt’ file using the dump method: