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.
json.dumps() takes in a json object and returns a string.
As you can see, json.dumps()
and json.loads()
are opposite of one another.
In this example, a string is converted into a dict
and the key age
is accessed in that json
object:
import json x = '{ "name":"John", "age":30, "city":"New York"}' y = json.loads(x) print(y["age"])
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:
import json a = {'lalalala': 3} myString = json.dumps(a) print (myString)
RELATED TAGS
View all Courses