How to Convert a Python List to JSON?

A popular data interchange format for web applications and APIs is JSON or JavaScript Object Notation. Python lists can be converted to JSON for human-readable data interchange.

To convert a list to JSON, the “json.dumps()” function and the “json.JSONEncoder().encode()” function are used in Python, which is briefly explained in this post: 

Method 1: Using the json.dumps() Function

A Python object can be converted into a JSON string using the “json.dumps()” function. The “json.dumps()” function serialized a given object into a JSON string. Below is a syntax for “json.dumps()”:

json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

In the above syntax, the parameter “obj” specifies the serialized object. There are optional additional parameters that can be used to customize the serialization process, such as for sorting dictionary keys, the “sort_key” parameter is used; for indentation “indent” parameter is used, etc.

Let’s understand the conversion of a Python list to JSON using various examples:

Example 1: List to JSON

The below code is used to convert the list to json:

Code:

import json
json_string = json.dumps([45, 55, 60])
print(json_string)
  • The module named “json” is initialized.
  • The “json.dumps()” function accepts the list as an argument/parameter and returns the JSON string.

Output:

A JSON representation of the input list has been created.

Example 2: List of Dictionaries to JSON

The below code is used to convert the list of dictionaries to JSON:

Code:

import json
json_string = json.dumps([{'Name': 'Joseph', 'Age': 22}, {'Name': 'Lily', 'Age': 24}])
print(json_string)

The “json.dumps()” function takes the list of dictionaries as a parameter and retrieves the JSON strings.

Output:

JSON has been created from the input list of dictionaries.

Example 3: List of Lists to JSON

The following example code is utilized to convert the list of lists to JSON:

Code:

import json
json_string = json.dumps([[23, 12, 55], [12, 3, 5], [1, 5, 22]])
print(json_string)

The “json.dumps()” function takes the list of lists as an argument and converts it into JSON strings.

Output:

A JSON string has been created from the input list of lists.

Method 2: Using the json.JSONEncoder().encode()

The “json.JSONEncoder().encode()” function is utilized to convert the Python list to JSON string. The “JSONEncoder” class is used to define the encoding of custom objects, and the “encode()” method is used to encode the object into a JSON string. 

Example:

Here is an example code:

Code:

import json
json_string = json.JSONEncoder().encode([34, 35, 15, 22])
print(json_string)
  • The module named “json” is imported at the start.
  • The “json.JSONEncoder().encode()” accepts the list as an argument/parameter and converts it into a JSON string.

Output:

A JSON representation of the input list has been created.

Conclusion

To convert a list, list of dictionaries, or list of lists to JSON, the “json.dumps()” function and the “json.JSONEncoder().encode()” function are used in Python. The “json.dumps()” function takes the list, list of lists, and list of dictionaries as a parameter and converts them into JSON strings. Similarly, the “json.JSONEncoder().encode()” function can convert the list to JSON strings. This post presented various methods to convert a Python list to JSON using numerous examples.