a shot of dev knowledge

RELATED TAGS

What is the difference between json.loads() and json.dumps()?

svg viewer

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.

svg viewer

Example json.loads()

In this example, a string is converted into a json object 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"])

Example json.dumps()

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

RELATED COURSES

View all Courses

Keep Exploring