Virtually each communication between server-side and client-side purposes is finished by means of JSON these days. In Python, a module named “json” accommodates varied capabilities that assist the consumer carry out JSON-related operations on the info. A type of capabilities is the “dumps()” technique, which is used to transform a listing right into a “JSON String.”
This publish will clarify the usage of the dumps() technique to transform varied kinds of lists into JSON strings.
Find out how to Use “json.dumps()” Methodology?
As defined earlier, the dumps() technique belongs to the “json” library and is used to transform a Python listing right into a JSON String. To know the use and dealing of this technique, check out its syntax beneath:
From the syntax, it’s clear that it solely takes one argument, which is the listing variable to be transformed right into a JSON string. To know the usage of this technique, go over the beneath examples.
Instance 1: Find out how to Convert an Integer Record into JSON?
Begin by importing the “json” package deal into this system after which initialize an integer with the next strains of code:
import json
intList = [55,12,74,36,9]
After that, go the newly created “intList” variable to the dumps() technique and retailer the outcome inside a brand new variable:
jsonString = json.dumps(intList)
As soon as that’s carried out, print out the kind of the “jsonString” variable to confirm that it isn’t transformed right into a JSON String and in addition print out its worth by passing the variable to the print perform:
print(kind(jsonString))
print(jsonString)
The code snippet for this instance is as follows:
intList = [55,12,74,36,9]
jsonString = json.dumps(intList)
print(kind(jsonString))
print(jsonString)
When this code is executed, it produces the next consequence:
From the output on the terminal, it’s observable that the listing is now transformed to a JSON string.
Instance 2: Find out how to Convert String Record to JSON?
The method of changing a listing containing strings or characters is virtually the identical as talked about within the earlier instance. To exhibit this instance, take the next code snippet:
StringList = [“LinuxHint”,“John Doe”,“Marcel”,“LiverPool”]
jsonString = json.dumps(StringList)
print(kind(jsonString))
print(jsonString)T
When this code snippet is executed, it produces the next outcomes on the terminal:
The output exhibits that the listing of strings has been transformed right into a JSON String.
Instance 3: Find out how to Convert a Record of Dictionaries Into JSON?
The dumps() technique can be used to transform a listing containing Python dictionaries into JSON strings. To showcase this, take the next code:
dictList = [{‘Name’:‘Marci’,‘age’:’25’},{‘domain’:‘Linuxhint’},{‘Game’:‘Dota’,‘Genre’:‘MMO’}]
jsonString = json.dumps(dictList)
print(kind(jsonString))
print(jsonString)
Within the listing variable, there are three totally different dictionaries. When this code snippet is executed, it produces the next consequence on the terminal:
The listing has been efficiently transformed right into a JSON string, and that was all about changing Python Lists to JSON utilizing the dumps() technique.
Conclusion
The dumps() technique from the json package deal converts a Python listing right into a JSON string. This technique takes in just one argument, which is the listing variable. Nonetheless, this variable can include any kind of worth, which implies that it may be a listing of integers, strings, dictionaries, or perhaps a listing of lists. To confirm that the output has been transformed right into a JSON string, the consumer can use the kind() technique throughout the print assertion.