Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

json.dumps
json.loads
difference

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

Educative Answers Team
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.

Example json.loads()

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"])

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

json.dumps
json.loads
difference
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring