PyPixel
Posted on September 10, 2023
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
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"]
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)
At the end, you can print the json_data
variable that we declared, by adding the print
command:
print(json_data)
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)
Output:
['Amit', 'Raghav', 'Utkarsh', 'Akash']
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)
Output:
[['Amit', 'Raghav', 'Utkarsh', 'Akash'], ['Delhi', 'Mumbai', 'Hyderabad', 'Chennai']]
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 cities
as 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)
When you run this file, the output will look something like this:
Output:
[{"Amit": "Delhi"}, {"Utkarsh": "Hyderabad"}, {"Raghav": "Mumbai"}]
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
Posted on September 10, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 3, 2024
September 6, 2024