DEV Community

Cover image for How to Convert a List to JSON Python? Best Simple Technique
PyPixel
PyPixel

Posted on • Originally published at pypixel.com

How to Convert a List to JSON Python? Best Simple Technique

Converting a List to JSON Python is fairly easy. JSON, which stands for JavaScript Object Notation, is a lightweight data-interchange format that is easy for both humans to read and write, and for machines to parse and generate. In this article, we’ll walk through the process of converting a Python list to a JSON object, exploring the concept step by step.

Understanding JSON and Its Importance

Before heading to the conversion process, let’s have a quick look at JSON and its significance. JSON is widely used for data exchange between a server and a web application, configuration storage, and various other applications due to its simplicity and readability. It’s a collection of key-value pairs, where values can be strings, numbers, objects, arrays, booleans, or nulls. Its versatility and simplicity make it a preferred choice for data representation.

Converting a List to JSON Python

To convert a Python list to a JSON, we can make use of Python’s built-in json module, we do not need to explicitly install it. This module provides methods to serialize Python objects into JSON format and deserialize JSON data back into Python objects. Here’s a step-by-step guide:

Step 1: Importing the JSON Module

First we will be importing the Python’s in-built json module into your Python script by adding this line of code:

import json
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating a Sample Python List

Now just for an example we will create a dummy list that we will further used to convert list to json in python:

data = ["Amit", "Raghav", "Utkarsh", "Akash"]
Enter fullscreen mode Exit fullscreen mode

Step 3: Conversion

With the json module that we imported earlier, we can employ the json.dumps() function to transform the Python list into a JSON-formatted string. The json.dumps() function takes a list as an parameter and converts it into the JSON:

json_data = json.dumps(data_list)
Enter fullscreen mode Exit fullscreen mode

At the end, you can print the json_data variable that we declared, by adding the print command:

print(json_data)
Enter fullscreen mode Exit fullscreen mode

Here’s the entire code that we discussed, make sure that your code is in the same format, and run the code:

import json # import json

data = ["Amit", "Raghav", "Utkarsh", "Akash"] 
json_data = json.dumps(data) # dumping data to json from list with json.dumps()
print(data)
Enter fullscreen mode Exit fullscreen mode

Output:

['Amit', 'Raghav', 'Utkarsh', 'Akash']
Enter fullscreen mode Exit fullscreen mode

Converting a List of Lists to JSON Python

Converting the list of lists is the same as converting a single list, just instead of passing a single list as the parameter to json.dumps() function we will be passing a list of lists to it, and we will get the desired output like before a JSON string

To start, let’s create two lists one of person names and one of their current cities and keep them inside a single list.

import json

data = [["Amit", "Raghav", "Utkarsh", "Akash"], ["Delhi", "Mumbai", "Hyderabad", "Chennai"]]
json_data = json.dumps(data)

print(json_data)
Enter fullscreen mode Exit fullscreen mode

Output:

[['Amit', 'Raghav', 'Utkarsh', 'Akash'], ['Delhi', 'Mumbai', 'Hyderabad', 'Chennai']]
Enter fullscreen mode Exit fullscreen mode

You can see that we have converted a list of List to JSON Python and how easy it was.

Converting a Python List of Dictionaries to JSON

Another thing that we can do is Convert a Python List of Dictionaries to JSON in the same way.

We’ll make a List of Dictionaries in Python to get started. The Dictionaries will have Name as Keys and their corresponding citiesas their values. Then we will provide that list of dictionaries to json.dumps() function for the conversion process.

import json

data = [{"Amit": "Delhi"}, {"Utkarsh": "Hyderabad"}, {"Raghav": "Mumbai"}]
json_data = json.dumps(data)

print(json_data)
Enter fullscreen mode Exit fullscreen mode

When you run this file, the output will look something like this:

Output:

[{"Amit": "Delhi"}, {"Utkarsh": "Hyderabad"}, {"Raghav": "Mumbai"}]
Enter fullscreen mode Exit fullscreen mode

Great. we have converted a python list of dictionaries to json.

Conclusion

In this Article, we have converted list to json python of 3 different aspects: List to JSON, List of Lists to JSON, List of Dictionaries to JSON. You can try implementing these in your code editor on your own and see if you’re able to match the outputs. If you have any doubts or things to add, you can comment below with that.

StackOverflow Resource:

https://stackoverflow.com/questions/37661863/convert-a-list-to-json-objects

Read More:

How to use Streamlit Library in Python to create Web Apps?
5 Simple Methods to Reverse a List in Python

Originally Posted on - PyPixel

Top comments (0)