In this tutorial, we are going to discuss how we can convert a Python list to JSON. JSON is one of the most popular data formats to represent structured data and uses JavaScript notation to store and exchange textual data. Let’s get started!
Also read: How to Read a JSON File in Python
Importing the JSON Module
We require json
Python module which is used to work with JSON objects and files. It contains the dumps()
function which we will be using here to convert a Python list to JSON. The json
module in Python is a standard Python package which comes with the normal Python interpreter installation. Hence, we do not have to install it manually on our local system. The dumps()
function takes a Python list as its parameter, converts it into JSON string, and then returns that JSON string. The syntax for using the dumps()
function to convert a Python list to JSON string:
# Import the json Python module
import json
# Call the dumps() function and pass the Python list
json_str = json.dumps(list)
Now, let’s discuss how we can convert the several types of Python lists i.e. simple list, list of lists, and list of dictionaries to JSON string.
Convert a list to JSON
First, we will see how to convert a simple Python list to JSON string. We will create a simple Python list of even numbers and then pass it to the above discussed json.dumps()
function which will convert the Python object (list) to JSON string. Let’s see how we can implement this using Python code.
# Import json Python module
import json
# Create a Python list
list_1 = [2, 4, 6, 8, 10]
# Convert the above Python list to JSON string
json_str_1 = json.dumps(list_1)
# Check the type of value returned by json.dumps()
print(type(json_str_1))
# Print the result
print(json_str_1)
Output:
<class 'str'>
[2, 4, 6, 8, 10]
Convert a list of lists to JSON string
Second, we will discuss how to convert a Python list of lists to JSON string. We will create a Python list of lists containing the capital English alphabet characters and their corresponding ASCII codes. Then we pass it to the json.dumps()
function which will convert the Python list of lists to JSON string. Let’s write the Python code to implement this.
# Import json Python module
import json
# Create a Python list of lists
list_2 = [['A', 'B', 'C', 'D', 'E'],
[65, 66, 67, 68, 69]]
# Convert the above Python list of lists to JSON string
json_str_2 = json.dumps(list_2)
# Check the type of value returned by json.dumps()
print(type(json_str_2))
# Print the result
print(json_str_2)
Output:
<class 'str'>
[["A", "B", "C", "D", "E"], [65, 66, 67, 68, 69]]
Convert a list of dictionaries to JSON string
Third, we will see how to convert a Python list of dictionaries to JSON string. We will create a Python list of dictionaries containing the Odd, Even, and Prime numbers’ lists corresponding to their keys (labels). Then we pass it to the json.dumps()
function which will convert the Python list of dictionaries to JSON string. Let’s implement this through Python code.
# Import json Python module
import json
# Create a Python list of dictionaries
list_3 = [{'Odd': [1, 3, 5, 7, 9]},
{'Even': [2, 4, 6, 8, 12]},
{'Prime': [2, 3, 5, 7, 11]}]
# Convert the above Python list of dictionaries to JSON string
json_str_3 = json.dumps(list_3)
# Check the type of value returned by json.dumps()
print(type(json_str_3))
# Print the result
print(json_str_3)
Output:
<class 'str'>
[{"Odd": [1, 3, 5, 7, 9]}, {"Even": [2, 4, 6, 8, 12]}, {"Prime": [2, 3, 5, 7, 11]}]
Conclusion
In this tutorial, we have learned how we can convert several types of Python list to JSON string using the json
Python module and its dumps()
function. Hope you have understood the concepts discussed above and ready to try them on your own. Thanks for reading this article! Stay tuned with us for more such amazing learning contents related to Python programming.