25 Apr How to Work with JSON Data in Python
JSON stands for JavaScript Object Notation which was derived from the subdivision of Javascript Programming Language dealing with Object Literal Syntax. It is used for representing structured data and is very effective for transmitting and receiving data between a server and web application.
JSON is a built-in package that came with Python. This package is very good for encoding and decoding JSON data.
Now, we’ll learn about 2 different terms in JSON, we have Serialization and Deserialization.
Serialization can be referred to as a way of encoding JSON, and how we use the dump()
method for writing data to files. Deserialization on the other hand means the conversion of JSON objects into their respective Python objects where we use the load()
and loads()
method to change the JSON encoded data into Python Objects.
We will go further to carry out some implementation below;
How to Convert JSON TO Dictionary
We will look into how we can convert our JSON data into a dictionary format by using the JSON module and the JSON.loads() method. An example of how to convert a JSON to a dictionary is shown below;
1 #import json module 2import json 3 4#our json string 5user = '{"member": "registered", "status": ["paid", "active"]}' 6#we use the .loads() method to convert our data 7user_dict = json.loads(user) 8#here we print the dictionary 9print(user_dict) 10# result: {"member": "registered", "status": ["paid", "active"]} 11 12print(user_dict['status']) 13# Output: ["paid", "active"]
How to Convert Dictionary to JSON
We can do a vice-versa of converting dictionary to JSON format as well. To convert a dictionary to JSON format, we will use the JSON module .dumps() method to handle this as shown below:
1#import json module 2import json 3#our dictionary 4user_dict = {'name': 'Todd', 5'age': 28, 6'married': None 7} 8#use the dumps method to change the dictionary back to json 9user_json = json.dumps(user_dict) 10#print our result 11print(user_json) 12# Output: {"name": "Todd", "age": 28, "married": null} 13
How to Read a JSON File
We all must have had a struggle reading a JSON file that possibly might not return what we desired. Now, we’ll demonstrate an example of reading a JSON file in Python by using the .load() method.
How to Write JSON to a file
What we need to do is finally test how we can write out our JSON data into any file. We’ll demonstrate that by writing JSON data to a text format with the example code below.
1#import json module 2import json 3#the user details 4user_dict = {'name': 'Todd', 5'age': 28, 6'married': None 7} 8#we open an empty user.txt file here before we begin to dump the data 9with open('user.txt', 'w') as json_file: 10 json.dump(user_dict, json_file)
Pretty Print Our JSON Data
We can also pretty print our JSON data to give it an appropriate structure, indenting, whitespace, and makes it look neater than the original form.
An example of how we can implement this is shown below:
1import json 2 3user_data = '{"member": "registered", "status": ["paid", "active"]}' 4 5# Getting dictionary 6user_dict = json.loads(user_data) 7 8# Pretty Print the user dict data 9print(json.dumps(user_dict, indent = 4, sort_keys=True))
If you found this post helpful, let us know in the comments box.
No Comments