In this article, we will discuss how to convert a list of dictionaries to JSON in python
This function will convert a list of dictionaries to JSON.
Syntax:
json.dumps(dict, indent)
Parameters:
- dictionary – name of a list of dictionaries which should be converted to JSON object.
- indent – defines the number of units for indentation (spaces)
Example: Python program to create a list of dictionaries of employee data and convert to JSON
Python3
import json
data = [{ "id" : ( "1" , "2" , "3" ), "name" : ( "bhanu" , "sivanagulu" ),
"department" : ( "HR" , "IT" )},
{ "id" : ( "4" , "5" , "6" ), "name" : ( "sai" , "poori" ),
"department" : ( "HR" , "IT" )},
{ "id" : ( "7" , "8" , "9" ), "name" : ( "teja" , "gowtam" ),
"department" : ( "finance" , "IT" )},
{ "id" : ( "10" , "11" , "12" ), "name" : ( "sai" , "jyothi" ),
"department" : ( "business" , "IT" )},
{ "id" : ( "13" , "14" , "15" ), "name" : ( "prudhvi" , "nagendram" ),
"department" : ( "business" , "IT" )}]
final = json.dumps(data, indent = 2 )
print (final)
|
Output:
[
{
"id": [
"1",
"2",
"3"
],
"name": [
"bhanu",
"sivanagulu"
],
"department": [
"HR",
"IT"
]
},
{
"id": [
"4",
"5",
"6"
],
"name": [
"sai",
"poori"
],
"department": [
"HR",
"IT"
]
},
{
"id": [
"7",
"8",
"9"
],
"name": [
"teja",
"gowtam"
],
"department": [
"finance",
"IT"
]
},
{
"id": [
"10",
"11",
"12"
],
"name": [
"sai",
"jyothi"
],
"department": [
"business",
"IT"
]
},
{
"id": [
"13",
"14",
"15"
],
"name": [
"prudhvi",
"nagendram"
],
"department": [
"business",
"IT"
]
}
]
This will write converted JSON data into a file.
Syntax:
json.dump(dict, file_pointer)
Parameters:
- dictionary – the name of dictionary which should be converted to JSON object.
- file pointer – pointer of the file opened in write or append mode.
Syntax:
with open("mydata.json", "w") as final:
json.dump(data, final)
where mydata is the new JSON file. Finally, we have to download the created JSON file
Syntax:
files.download('mydata.json')
Example:
Python3
from google.colab import files
import json
data = [{ "id" : ( "1" , "2" , "3" ), "name" : ( "bhanu" , "sivanagulu" ),
"department" : ( "HR" , "IT" )},
{ "id" : ( "4" , "5" , "6" ), "name" : ( "sai" , "poori" ),
"department" : ( "HR" , "IT" )},
{ "id" : ( "7" , "8" , "9" ), "name" : ( "teja" , "gowtam" ),
"department" : ( "finance" , "IT" )},
{ "id" : ( "10" , "11" , "12" ), "name" : ( "sai" , "jyothi" ),
"department" : ( "business" , "IT" )},
{ "id" : ( "13" , "14" , "15" ), "name" : ( "prudhvi" , "nagendram" ),
"department" : ( "business" , "IT" )}]
with open ( "mydata.json" , "w" ) as final:
json.dump(data, final)
files.download( 'mydata.json' )
|
Output:
[{“id”: [“1”, “2”, “3”], “name”: [“bhanu”, “sivanagulu”], “department”: [“HR”, “IT”]}, {“id”: [“4”, “5”, “6”], “name”: [“sai”, “poori”], “department”: [“HR”, “IT”]}, {“id”: [“7”, “8”, “9”], “name”: [“teja”, “gowtam”], “department”: [“finance”, “IT”]}, {“id”: [“10”, “11”, “12”], “name”: [“sai”, “jyothi”], “department”: [“business”, “IT”]}, {“id”: [“13”, “14”, “15”], “name”: [“prudhvi”, “nagendram”], “department”: [“business”, “IT”]}]