Convert a List to JSON String in Python [Easy Step-By-Step]

Convert A List To JSON String

JSON is a widely used format for data exchange between different systems and programming languages. It uses JavaScript notation to store and exchange textual data. It is small in size making it fast to transfer over the network for the transmission of information.

By converting a Python list to JSON, one can utilise its powerful features. We can use the Python json module to convert the Python list to a JSON string.

In this tutorial, we will see a method to convert a list to a JSON string using json module. We’ll also learn how to convert a list of lists and a list of dictionaries to a JSON string. With that being said, let’s dive deeper into the interesting content.

Also Read: How to Read a JSON File in Python


Using JSON Module

The json module in Python is used to work with JSON objects and files. It is a standard Python package that comes with the Python. Hence, we do not have to install it manually on our local system. We can directly import in by using the import statement.

 import json

The json module has a function dumps() that is used to convert a list to JSON in Python.

JSON Module dumps() Function 

The dumps() function takes a Python list as its parameter, converts it into a JSON string, and then returns that JSON string. The syntax for using the dumps() function is given below.

Syntax:

json_str = json.dumps(list)

where the list is a list that you want to convert to a JSON string.

Now, let’s discuss how we can convert the several types of Python lists i.e. a simple list, list of lists, and list of dictionaries to JSON string.

Convert a List to JSON

Let’s start with converting a simple Python list of even numbers. We have to pass the list to the above-discussed json.dumps() function to convert into JSON data string. 

Example:

# 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)

Here we have used the type() function to get the data type of the result returned by the json.dumps() function.

Output:

<class 'str'>
[2, 4, 6, 8, 10]

In the above output, you are not seeing any difference because the list passed is already a valid JSON array.

Convert a List of Lists to JSON

We can convert a list of lists to a JSON string in the same way as we have done for a list of numbers.

To demonstrate this conversion, we will first create a Python list of lists containing the capital English alphabet characters and their corresponding ASCII codes. Then we will pass it to the json.dumps() function which will return the required JSON string. 

Example:

# 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]]

In the above output,  you can see that the data type of the returned value is a string which shows that we have successfully converted the list of lists into a JSON string.

Convert a List of Dictionaries to JSON string

We can also convert list of dictionaries to a JSON string by using json.dumps() function.

For example, we will create a Python list of dictionaries containing the odd, even, and prime numbers’ lists corresponding to their keys (labels). Then we will pass it as an argument to the json.dumps() function and print the resultant value in the console.

Example:

# 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]}]

See, we have successfully converted the list of dictionaries into a JSON string.

Conclusion

In this tutorial, we have learned that to convert list to JSON, we can use the json.dumps() method of json module, which takes a list as an argument and returns a json string. This method can convert not only a list of values but also a list of lists and a list of dictionaries into a JSON string. Hope you have understood the concepts discussed above and are ready to try them on your own. Thanks for reading this article! Stay tuned for more such amazing learning material related to Python programming. 

Reference

https://stackoverflow.com/questions/37661863/convert-a-list-to-json-objects