Skip to content

Convert Python list to JSON | Example code

  • by

Use json.dumps() function to convert a list to JSON in Python. This function takes a list as an argument and returns a JSON String.


import json

jsonString = json.dumps(list)

Python list to JSON example

A simple example code takes a Python list with some numbers in it and converts it to a JSON string.

import json

aList = [1, 2, 3]
res = json.dumps(aList)



Convert Python list to JSON

How to Convert Python List of Dictionaries to JSON?

Answer: Same method son.dumps() will work for converting a list of dict into a JSON.

import json

aList = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
res = json.dumps(aList)


Output: [{“a”: 1, “b”: 2}, {“c”: 3, “d”: 4}]

Convert Python List of Lists to JSON

You can convert the list into JSON using the dumps() method. First import the json module then use the method.

import json

aList = [[{'a': 1, 'b': 2}], [{'c': 3, 'd': 4}]]
res = json.dumps(aList)


Output: [[{“a”: 1, “b”: 2}], [{“c”: 3, “d”: 4}]]

Do comment if you have any doubts or suggestions on this Python List tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *