Convert List to JSON Object & Vice Versa in Python (Examples)
Hi! This tutorial will show you how to turn a list to a JSON object and vice-versa in the Python programming language.
First, though, here is an overview of this tutorial:
Let’s get right into it!
Create Sample List
Here, we will create the sample Python list that will be transformed into a JSON object. So, in your Python IDE, run the line of code below to create the sample list:
my_list = [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>
Import Built-in json Package
Python comes with a built-in JSON package. Therefore, we only need to import the package in order to use its functions:
import json
Example 1: List to JSON | Turn List to JSON Object Using json.dumps() Function
In this example, we will transform the list to a JSON object, using the json.dumps() function:
json_obj = json.dumps(my_list) print(json_obj) # [1, 2, 3, 4, 5] print(type(json_obj)) # <class 'str'>
The json.dumps()
function takes the list as its argument and outputs a JSON string.
Example 2: List to JSON | Turn List to JSON Object Using Dictionary & json.dumps() Function
In this next example, we will use a Python dictionary and the json.dumps()
function to turn the list to a JSON object:
my_dict = {"list": my_list} json_obj2 = json.dumps(my_dict) print(json_obj2) {"list": [1, 2, 3, 4, 5]} print(type(json_obj2)) # <class 'str'>
In the example above, we, first of all, turned the list into a dictionary, after which we used the json.dumps()
function to transform the dictionary into a json string.
Having seen how to turn a list into a JSON object, we will now examine ways to convert the JSON object back into a Python list.
Example 1: JSON to List | Turn JSON Object to List Using json.loads() Function
In this example, we will use the json.loads() function to turn the json object back into a list:
new_list = json.loads(json_obj) print(new_list) # [1, 2, 3, 4, 5] print(type(new_list)) # <class 'list'>
We simply parsed the JSON object to the json.loads()
function, and it returned a list object.
Example 2: JSON to List | Turn JSON Object to List Using ast.literal_eval() Function
In this second example, we will use the ast.literal_eval() function from Python’s built-in ast package to convert the JSON object back to a list:
import ast new_list = ast.literal_eval(json_obj) print(new_list) # [1, 2, 3, 4, 5] print(type(new_list)) # <class 'list'>
In the example, we parsed the JSON object to the ast.literal_eval()
function, which then returned a Python list object.
With that, we have demonstrated how to convert a list to a JSON object and vice-versa in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a list to a JSON object and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a list to a JSON object and vice-versa in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Convert List of Multiple Lists to Single List in Python (3 Examples)
- Check if List of Lists is Empty in Python (2 Examples)
- Lists in Python (13 Examples)
- Convert List to Matrix & Vice-Versa in Python (Examples)
- Convert Python List to PyTorch Tensor & Vice-Versa in Python (Examples)
- Learn Python Programming
This post has shown how to convert a list to a JSON object and vice-versa in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter