top of page

Difference between json.dumps() and json.loads()

Chandan Rajpurohit
Jan 21, 2024
2 min read

JSON is important for many backend devs. But sometimes playing with it can be confusing.


Being a backend dev or full stack dev you might have written APIs for your Web or Mobile application and I’m sure you might have come across json.dumps() and json.loads().


Let’s understand the difference between json.dumps() and json.loads() with an example.


json.dumps()


import json

user_details_dict = {
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}

user_details_string = """{
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}"""

user_details_dict_json_dumps = json.dumps(user_details_dict)
user_details_string_json_dumps = json.dumps(user_details_string)

# user_details_dict_json_loads = json.loads(user_details_dict)
# user_details_string_json_loads = json.loads(user_details_string)

print(user_details_dict)
print(type(user_details_dict))

print(user_details_string)
print(type(user_details_string))

print(user_details_dict_json_dumps)
print(type(user_details_dict_json_dumps))

print(user_details_string_json_dumps)
print(type(user_details_string_json_dumps))

# print(user_details_dict_json_loads)
# print(type(user_details_dict_json_loads))

# print(user_details_string_json_loads)
# print(type(user_details_string_json_loads))

In the above code example, I have 2 objects one of a type dictionary and the other of a type string.


json.dumps() returns a string for both dict and string objects, as you can see in the below response.


{'first_name': 'Chandan', 'last_name': 'Rajpurohit'}
<class 'dict'>
{
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}
<class 'str'>
{"first_name": "Chandan", "last_name": "Rajpurohit"}
<class 'str'>
"{\n    \"first_name\": \"Chandan\",\n    \"last_name\": \"Rajpurohit\"\n}"
<class 'str'>
>

So basically, json.dumps() serialize(or encode) and returns a Python object into a string formatted JSON object. In simple terms, it converts a Python object into a string-formatted JSON object.


json.loads()


import json

user_details_dict = {
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}

user_details_string = """{
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}"""

# user_details_dict_json_dumps = json.dumps(user_details_dict)
# user_details_string_json_dumps = json.dumps(user_details_string)

user_details_dict_json_loads = json.loads(user_details_dict)
user_details_string_json_loads = json.loads(user_details_string)

print(user_details_dict)
print(type(user_details_dict))

print(user_details_string)
print(type(user_details_string))

# print(user_details_dict_json_dumps)
# print(type(user_details_dict_json_dumps))

# print(user_details_string_json_dumps)
# print(type(user_details_string_json_dumps))

print(user_details_dict_json_loads)
print(type(user_details_dict_json_loads))

print(user_details_string_json_loads)
print(type(user_details_string_json_loads))

Now when I run the above code I get a TypeError: the JSON object must be str, bytes or bytearray, not dict.


Traceback (most recent call last):
  File "<string>", line 16, in <module>
  File "/usr/local/lib/python3.11/json/__init__.py", line 339, in loads
ERROR!
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict
>

Why this happened?


Because json.loads() takes string-formatted JSON and as you can see in the above error it says that the JSON object must be str, bytes or bytearray, not a dict.


import json

user_details_dict = {
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}

user_details_string = """{
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}"""

# user_details_dict_json_dumps = json.dumps(user_details_dict)
# user_details_string_json_dumps = json.dumps(user_details_string)

# user_details_dict_json_loads = json.loads(user_details_dict)
user_details_string_json_loads = json.loads(user_details_string)

print(user_details_dict)
print(type(user_details_dict))

print(user_details_string)
print(type(user_details_string))

# print(user_details_dict_json_dumps)
# print(type(user_details_dict_json_dumps))

# print(user_details_string_json_dumps)
# print(type(user_details_string_json_dumps))

# print(user_details_dict_json_loads)
# print(type(user_details_dict_json_loads))

print(user_details_string_json_loads)
print(type(user_details_string_json_loads))

So when I pass string-formatted JSON to json.loads I get a response which is of type dict.


{'first_name': 'Chandan', 'last_name': 'Rajpurohit'}
<class 'dict'>
{
    "first_name": "Chandan",
    "last_name": "Rajpurohit"
}
<class 'str'>
{'first_name': 'Chandan', 'last_name': 'Rajpurohit'}
<class 'dict'>
>

json.loads() deserialize(or decode) and returns a string-formatted JSON to a Python object. In simple terms, it converts string-formatted JSON to a Python object.


In conclusion, when you want to convert string to dict use json.loads(), and when you want to convert dict to string use json.dumps().


Try the above code yourself and let me know your thoughts.

Recent Posts

See All
Babel 101: Writing Future JavaScript, Today

If you’ve spent any time in the JavaScript ecosystem, you’ve likely heard the name Babel come up again and again. It’s one of those tools that quietly powers a huge portion of the modern web, yet many

 
 
 
Difference between json and jsonb in PostgreSQL

PostgreSQL allows to store JSON data in json and jsonb datatype. Let’s understand the difference between json and jsonb. json jsonb json datatype was first introduced with Postgres 9.2. jsonb datatype

 
 
 
How to work with DataFrame in Python?

Python language has vast application. Data analysis and manipulation is one of many application. We can work on data using Pandas library in Python for generating data analytics and manipulating data

 
 
 

Comments


bottom of page