In this article, we will discuss how to convert a list of dictionaries to JSON in Python.
Python Convert List of Dictionaries to Json
Below are the ways by which we can convert a list of dictionaries to JSON in Python:
Dictionaries to JSON in Python Using json.dumps()
In this example, the Python JSON module is employed to convert a list to a JSON file Python representing employee data into a JSON-formatted string. The json.dumps() function is utilized with the indent parameter set to 2 for readability, generating a structured JSON output for the given data.
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"]
}
]
Convert List of Dictionaries to Json in Python Using json.dump()
In this example, the Python json module is utilized in a Google Colab environment to convert a list of dictionaries representing employee data into a JSON file named “mydata.json.” Here, we will convert python list of objects to json. The file is then downloaded using the files.download() function from the google.colab module.
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"]
}
]
Python Convert List of Dictionaries to Json Using json.JSONEncoder
In this example, the Python json module is employed to customize the serialization of a list of dictionaries. Here, we will convert python list of objects to json. The json.JSONEncoder class is subclassed, overriding the default method to convert tuple objects to lists during JSON encoding, resulting in a formatted JSON string with indents for improved readability.
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")}
]
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, tuple):
return list(obj)
return super().default(obj)
json_result_custom = json.dumps(data, cls=CustomEncoder, indent=2)
print(json_result_custom)
|
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"]
}
]
Handling Non-Serializable Types with default Parameter
In this example, the Python json module is utilized to handle non-serializable types within a list of dictionaries during JSON encoding. Here, we will convert list to json file python. The default parameter of json.dumps() is employed with a lambda function, converting tuple objects to lists and ensuring proper serialization, resulting in a formatted JSON string with indents for clarity
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")}
]
json_result_default = json.dumps(data, default=lambda x: list(x) if isinstance(x, tuple) else str(x), indent=2)
print(json_result_default)
|
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"]
}
]