How to Convert List to JSON in Python

To convert a list to JSON in Python, you can use the json.dumps() function. This function takes a list as an argument and returns a JSON string. For example, if you have a list called main_list = [1, 2, 3], json.dumps() returns json string [1, 2, 3].

import json

data = ["DisneyPlus", "Netflix", "Peacock"]
json_string = json.dumps(data)
print(json_string)

Output

["DisneyPlus", "Netflix", "Peacock"]

You can see that we converted json string to a list using the dumps() method.

How to Convert a List of Dictionaries to JSON

You can use the json.dumps() function to convert a list of dictionaries to JSON. This function preserves the original data and structure of the list of dictionaries.

import json

list_of_dicts = [{'kb':19, 'kl':21}, {'mbb':11, 'sm':18}]
json_str = json.dumps(list_of_dicts)
print(json_str)

Output

[{"kb": 19, "kl": 21}, {"mbb": 11, "sm": 18}]

You can see that we converted a list of dictionaries to json string without losing any original data.

How to Convert a List of Lists to JSON

You can use the json.dumps() method to convert a list of lists to json string in Python. It accepts the list of lists as an argument and returns the json string without losing input data.

import json

list_of_lists = [[19, 21], [11, 18], [46]]
json_str = json.dumps(list_of_lists)
print(json_str)

Output

[[19, 21], [11, 18], [46]]

How to Write JSON data to a file in Python

To write a json data into a file, use the with open() function in the “w” mode and then use the json.dump() method to write all the content in the file.

Let’s implement a program to write the JSON data into a file.

import json

data = {"Eleven": "Millie",
        "Mike": "Finn",
        "Will": "Noah"}

with open('app.json', 'w') as f:
  json.dump(data, f)

Output

In your app.json file, you have the data json written.

If we want to get the utf8-encoded, then write the following code.

import json

data = {"Eleven": "Millie",
        "Mike": "Finn",
        "Will": "Noah"}

with open('app.json', 'w', encoding='utf-8') as f:
  json.dump(data, f, ensure_ascii=False, indent=4)

The following indented output will be written in the file.

Output

{
   "Eleven": "Millie",
   "Mike": "Finn",
   "Will": "Noah"
}

We can get the utf8-encoded file instead of ASCII-encoded using the following code.

import json

data = {"Eleven": "Millie",
        "Mike": "Finn",
        "Will": "Noah"}

with open('app.json', 'w', encoding='utf-8') as f:
  f.write(json.dumps(data, ensure_ascii=False))

Output

{"Eleven": "Millie", "Mike": "Finn", "Will": "Noah"}

On Windows, the encoding=’utf-8′ argument to open is still necessary.

We can avoid storing an encoded copy of the data in memory (the result of dumps), and to output utf8-encoded bytestrings in Python 2 and 3, use the following code.

import json, codecs

with open('app.txt', 'wb') as f:
  json.dump(data, codecs.getwriter('utf-8')(f), ensure_ascii=False)

The codecs.getwriter() function call is redundant in Python 3 but required for Python 2.

Conclusion

The fast and efficient way to convert a list to json in Python is to use the built-in json.dumps()  function. You can use the json.dump() function to write a json data to a file.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.