Python List to JSON

JSON is a very well-known and quite frequently used data format of Python language that is used to signify different data structures of Python. As we know, there are a lot of data structures available in Python. JSON works by utilizing JavaScript notation to save and converse textual data in Python. Thus, we will see some examples of how a “list” data structure can be converted into JSON using Spyder 3 in Windows 10. So, let’s get started.

Example 01: String Type List to JSON

Create a new project in Spyder 3. Let us begin our first example by importing the JSON module in the code area. We have initialized a string type list with three elements in it.

JSON provides us with the method “dumps()” to convert a data structure to JSON data type. Therefore, we have been using the “dumps()” method here by JSON object to convert a list to JSON. The list has been passed into its parameters, and the converted data will be stored in a new variable, “json_string”. We will be printing the type of a converted new variable by applying the type() method and then the “print()” method on it. In the end, the variable “json_string” would be printed out. The example code is available in the image below:

Tap on the “run” button to execute this code. The output is specified below. It shows that the converted new variable is of “string” type, and the whole string JSON type data structure has been printed out:

Example 02: Integer Type List to JSON

Let’s have another example to convert an integer type list to JSON data. Start by importing the JSON module. The integer type list “L” has been initialized with five values. Type of a list “L” would be found out by type method and then printed out. After this, the original list “L” would be passed to the JSON’s “dumps()” method to be converted into JSON data using a JSON object. The resulting JSON type data would be saved in the variable “s”.

At last, the type of the variable “s” and its value would be displayed by the “print” clause of Python. The example code is available in the following image:

When we run this code, it will first output the type of Original data structure “L” and then display the type of newly obtained by conversion, i.e., “s”. In the end, the newly obtained JSON variable’s data would be displayed on the console. The output of the previously presented code can be checked in the following image:

Example 03: List of Dictionaries to JSON

Within our new example, we will be converting a list of dictionaries into JSON data. So, we have started with importing the JSON module into our code. Then, we have initialized a list of dictionaries named “L” in the code. This list contains two dictionaries. First, print the type of original list “L” and then convert it into JSON by using the “dumps()” method on it.

The newly obtained variable’s type and value would be displayed on the console. The example code is available in the image below:

Run the file by tapping on the “Run” button from the menu bar of Spyder 3. The output shows that the original list is of “list” type while the new one is of “str” type. The output of the previously presented code can be checked from the underneath image:

Example 04: List of Lists to JSON

We have just updated the previous code list by using the list within the list “L” instead of using dictionaries. The list “L” now contains four lists in it. The remaining code is similar, as described in the preceding example. The example code is available in the following image:

The output is the same as above. The only change is the List. The output of the previously presented code can be checked from the image below:


Within this guide, we have seen converting different sorts of “lists” to JSON data. We have utilized string type, integer type, list of dictionaries, and list of lists for this purpose. Four different examples have been elaborated on to get a better idea of this article. We hope you will find this content to be useful.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content