json objects are surrounded by curly braces { }. They are written in key and value pairs.
json.loads() : takes in a string and returns a json object.
Example json.loads()
import json
x = '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)
print(y["age"])
output: 30
In this example, a string is converted into a json object and the key age is accessed in that json object.
json.dumps() : takes in a json object and returns a string.
import json
a = {'cppSecrets': 3}
myString = json.dumps(a)
print (myString)
output: {"cppSecrets": 3}
In this example, a json
object is passed in the json.dumps()
function and its data is extracted and returned in the form of a string.
Conclusion:json.dumps()
and json.loads()
are opposite of one another.
Comments