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 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"]
}
]
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
# 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"]
}
]
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")}
]
# Using json.JSONEncoder for customization
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")}
]
# Handling non-serializable types with the default parameter
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"]
}
]
Similar Reads
Convert JSON to dictionary in Python JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the Python JSON package into Pytho
4 min read
Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read
Convert nested Python dictionary to object Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. python3 # importing the module import json # declaringa a class class obj: #
2 min read
How To Convert Python Dictionary To JSON? In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
6 min read
Convert List Of Tuples To Json Python Working with data often involves converting between different formats, and JSON is a popular choice for data interchange due to its simplicity and readability. In Python, converting a list of tuples to JSON can be achieved through various approaches. In this article, we'll explore four different met
3 min read