Monday, September 23, 2024
Google search engine
HomeLanguagesPython – Convert list of dictionaries to JSON

Python – Convert list of dictionaries to JSON

In this article, we will discuss how to convert a list of dictionaries to JSON in python

Method 1: Using json.dumps()

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 module
import json
 
# list of dictionaries of employee data
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")}]
 
 
# convert into json
final = json.dumps(data, indent=2)
 
# display
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"
    ]
  }
]

Method 2: Using json.dump()

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




# import json module
from google.colab import files
import json
 
# list of dictionaries of employee data
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")}]
 
 
# convert into json
# file name is mydata
with open("mydata.json", "w") as final:
    json.dump(data, final)
 
# download the json file
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”]}]

RELATED ARTICLES

Most Popular

Recent Comments