Almost every communication between server-side and client-side applications is done through JSON nowadays. In Python, a module named “json” contains various functions that help the user perform JSON-related operations on the data. One of those functions is the “dumps()” method, which is used to convert a list into a “JSON String.”
This post will explain the use of the dumps() method to convert various types of lists into JSON strings.
How to Use “json.dumps()” Method?
As explained earlier, the dumps() method belongs to the “json” library and is used to convert a Python list into a JSON String. To understand the use and working of this method, take a look at its syntax below:
From the syntax, it is clear that it only takes one argument, which is the list variable to be converted into a JSON string. To understand the use of this method, go over the below examples.
Example 1: How to Convert an Integer List into JSON?
Start by importing the “json” package into the program and then initialize an integer with the following lines of code:
intList = [55,12,74,36,9]
After that, pass the newly created “intList” variable to the dumps() method and store the result inside a new variable:
Once that is done, print out the type of the “jsonString” variable to verify that it is not converted into a JSON String and also print out its value by passing the variable to the print function:
print(jsonString)
The code snippet for this example is as follows:
intList = [55,12,74,36,9]
jsonString = json.dumps(intList)
print(type(jsonString))
print(jsonString)
When this code is executed, it produces the following outcome:
From the output on the terminal, it is observable that the list is now converted to a JSON string.
Example 2: How to Convert String List to JSON?
The process of converting a list containing strings or characters is practically the same as mentioned in the previous example. To demonstrate this example, take the following code snippet:
StringList = ["LinuxHint","John Doe","Marcel","LiverPool"]
jsonString = json.dumps(StringList)
print(type(jsonString))
print(jsonString)
When this code snippet is executed, it produces the following results on the terminal:
The output shows that the list of strings has been converted into a JSON String.
Example 3: How to Convert a List of Dictionaries Into JSON?
The dumps() method can also be used to convert a list containing Python dictionaries into JSON strings. To showcase this, take the following code:
dictList = [{'Name':'Marci','age':'25'},{'domain':'Linuxhint'},{'Game':'Dota','Genre':'MMO'}]
jsonString = json.dumps(dictList)
print(type(jsonString))
print(jsonString)
In the list variable, there are three different dictionaries. When this code snippet is executed, it produces the following outcome on the terminal:
The list has been successfully converted into a JSON string, and that was all about converting Python Lists to JSON using the dumps() method.
Conclusion
The dumps() method from the json package converts a Python list into a JSON string. This method takes in only one argument, which is the list variable. However, this variable can contain any type of value, which means that it can be a list of integers, strings, dictionaries, or even a list of lists. To verify that the output has been converted into a JSON string, the user can use the type() method within the print statement.