Python: JSON module to read, write and manipulate JSON

Hello Everyone,

Youtube Video available Here

In this blog post I am going to explain you about how to write, read and manipulate JSON data using python.

In Python we have support for json by default. By using

import py

We can load all the libraries needed to work with JSON data.

Loading JSON data into python

We can use json.load() method to load the JSON object from external sources like database, files etc.

I have created a file locally with below content

file:sample.json

[
	{
		"python": {
		"type": "interpreter",
		"mode": "dynamic"
			},
		"c++" :{
		"type":  "compiler",
		"mode": "static"
			},
		"javascript" : {
			"type":  "interpreter",
			"mode": "dynamic"
			}
	}
]

And to load this JSON into python program use load method as below

In [2]: import json

In [3]: with open("sample.json", "r") as file:
...:     json_data = json.load(file)
...: 

And you can access specific object and attribute and their value as below

In [5]: json_data
Out[5]: 
[{'python': {'type': 'interpreter', 'mode': 'dynamic'},
'c++': {'type': 'compiler', 'mode': 'static'},
'javascript': {'type': 'interpreter', 'mode': 'dynamic'}}]

In [6]: json_data[0]
Out[6]: 
{'python': {'type': 'interpreter', 'mode': 'dynamic'},
'c++': {'type': 'compiler', 'mode': 'static'},
'javascript': {'type': 'interpreter', 'mode': 'dynamic'}}

In [7]: json_data[0]["python"]
Out[7]: {'type': 'interpreter', 'mode': 'dynamic'}

In [8]: json_data[0]["python"]["type"]
Out[8]: 'interpreter'

You can add new JSON data as well to existing object.

This code created entirely new object structure

In [9]: json_data.append( { "c" : {"type" : "interpreter", "mode": "static" } } );

In [10]: json_data
Out[10]: 
[{'python': {'type': 'interpreter', 'mode': 'dynamic'},
'c++': {'type': 'compiler', 'mode': 'static'},
'javascript': {'type': 'interpreter', 'mode': 'dynamic'}},
{'c': {'type': 'interpreter', 'mode': 'static'}}]

and this code created another JSON object in the same nested structure, observe the output you will figure it out, I am lazy to spoon feed :D

In [11]: json_data[0]["c"] = {"type" : "interpreter", "mode": "static" }

In [12]: json_data
Out[12]: 
[{'python': {'type': 'interpreter', 'mode': 'dynamic'},
'c++': {'type': 'compiler', 'mode': 'static'},
'javascript': {'type': 'interpreter', 'mode': 'dynamic'},
'c': {'type': 'interpreter', 'mode': 'static'}},
{'c': {'type': 'interpreter', 'mode': 'static'}}]

And now if you want to write this updated JSON object into a file you can use dump method of json module.

Writing JSON data from python

In [15]: with open("sample.json", "w") as file:
...:     json.dump(json_data, file , indent=4)

output file

In [16]: cat sample.json
[
	{
	"python": {
		"type": "interpreter",
		"mode": "dynamic"
		},
	"c++": {
		"type": "compiler",
		"mode": "static"
		},
	"javascript": {
		"type": "interpreter",
		"mode": "dynamic"
		},
	"c": {
		"type": "interpreter",
		"mode": "static"
		}
	},
	{
	"c": {
		"type": "interpreter",
		"mode": "static"
		}
	}
]

Hope it helps.

Thank you.

Comments

Popular posts from this blog

grep: unknown device method

Uploading files to FTP/SFTP using CURL

How to find outgoing IP in Linux ?