• Post author:
  • Post category:Python
  • Post last modified:March 27, 2024
  • Reading time:11 mins read

How to convert a list to JSON in Python? You can use the json.dumps() method to convert a Python list to a JSON string. This function takes a list as an argument and returns the JSON value. The json.dumps() function converts data types such as a dictionary, string, integer, float, boolean, and None into JSON. In this article, I will explain the syntax of how to convert a list to JSON in Python, its parameters, and explain how to use it.

1. Quick Examples of Convert List to JSON

If you are in a hurry, below are some quick examples of how to convert list to JSON.


# Quick examples of convert list to JSON

# Example 1: Convert python list to JSON
numbers = [25, 36, 48, 54]
json_numbers = json.dumps(numbers)

# Example 2: Convert python  string type list to JSON
technology = ["Hadoop", "Spark", "Python"]
json_string = json.dumps(technology)

# Example 3: Convert list of dictionaries to JSON
my_dict = [{'course':'python','fee':4000}, {'duration':'60days', 'discount':1200}]
jsondict = json.dumps(my_dict)

# Example 4: Convert a list of lists to JSON
List = [[{'course':'python','fee':4000}], [{'duration':'60days', 'discount':1200}]]
jsonList = json.dumps(List)

2. Syntax of json.dumps() Function

Following is a syntax of the json.dumps() function.


# Syntax of json.dumps() function
import json
json.dumps(list)

You have to import a JSON package to use json.dumps() function.

3. Convert List to JSON

You can use the JSON module to convert a list to a JSON object. The json.dumps() function converts a Python object, in this case, a list, into a JSON string.

In the below example, first import the JSON module. Create a Python list called numbers containing some integer elements. Use json.dumps() to convert the Python list (numbers) into a JSON-formatted string (json_numbers). Print the JSON-formatted string.


# Import json
import json

# Create list of elements
numbers = [25, 36, 48, 54]
print("Original list:\n", numbers)

# Convert python list to JSON
json_numbers = json.dumps(numbers)
print("Convert list to JSON:\n",json_numbers)

Yields below output.

python list json

Here’s a simple example of how to convert a Python list to a JSON-formatted string using the json.dumps() function. For instance, you can use the json.dumps() function to convert a Python list (technology) into a JSON-formatted string (json_string).


import json

# Create list of elements
technology = ["Hadoop", "Spark", "Python"]
print("Original list:\n", technology)

# Convert python string type list to JSON
json_string = json.dumps(technology)
print("Convert string type list to JSON:\n",json_string)

Yields below output.

python list json

The output shows the original list and the corresponding JSON-formatted string. The json.dumps() function is used to convert the list into a JSON-formatted string, and in this case, the resulting JSON string represents an array of strings.

4. Convert List of Dictionaries to JSON

The json.dumps() method can be used to convert a list of dictionaries to a JSON string in Python. This function converts a list of dictionaries to JSON string without losing any original data.


import json

# Create a list of dictionaries
my_dict = [{'course':'python','fee':4000}, {'duration':'60days', 'discount':1200}]
print("Original list:\n",my_dict)

# Convert list of dictionaries to JSON
jsondict = json.dumps(my_dict)
print("Convert list of dictionaries to JSON:\n",jsondict)

# Output:
#  Original list:
#  [{'course': 'python', 'fee': 4000}, {'duration': '60days', 'discount': 1200}]
# Convert list of dictionaries to JSON:
#  [{"course": "python", "fee": 4000}, {"duration": "60days", "discount": 1200}]

5. Convert a List of Lists to JSON

You can also use the json.dumps() method to convert a Python list of lists to a JSON string, and this process preserves the original data in the lists. For example, JSON string representation of the list of lists, which you can then write to a file or send over a network connection, etc.


import json

# Create a list of dictionaries
List = [[{'course':'python','fee':4000}], [{'duration':'60days', 'discount':1200}]]
print("Original list:\n",List)

# Convert a list of lists to JSON
jsonList = json.dumps(List)
print("Convert list of lists to JSON:\n",jsonList)

# Output:
# Original list:
#  [[{'course': 'python', 'fee': 4000}], [{'duration': '60days', 'discount': 1200}]]
# Convert list of lists to JSON:
#  [[{"course": "python", "fee": 4000}], [{"duration": "60days", "discount": 1200}]

Frequently Asked Questions

How do I convert a Python list to a JSON string?

You can use the json.dumps() function from the json module to convert a Python list to a JSON-formatted string

How can I make the JSON output more readable?

You can make the JSON output more readable by using the indent parameter of the json.dumps() function. The indent parameter specifies the number of spaces to use for indentation in the output JSON string.

Can I convert a list of dictionaries to JSON?

You can convert a list of dictionaries to JSON using the json.dumps() function in Python. For example, the list my_list_of_dicts contains dictionaries as its elements. The json.dumps() function is then used to convert this list to a JSON-formatted string. The indent=2 parameter is optional and adds indentation for better readability. The resulting JSON string represents an array of dictionaries. Each dictionary in the list is converted to a JSON object.

What if my list contains nested lists or dictionaries?

json.dumps() can handle nested structures as well. It will recursively convert nested lists and dictionaries to their JSON representations.

How can I ensure ASCII characters in the JSON output?

By default, the json.dumps() function in Python escapes non-ASCII characters using Unicode escape sequences (e.g., \uXXXX). If you want to ensure that the output contains only ASCII characters without Unicode escape sequences, you can use the ensure_ascii parameter and set it to True.

Conclusion

In this article, I have explained how to convert a list to JSON in Python by using json.dumps() function with examples.

Happy Learning !!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.