Introduction to Python JSON Method – Dumps()

In this article you will learn about Python JSON Method.

Python Json Method – Before moving ahead let’s know a little bit about Python Json.

JSON – It is Python built-in package, which is used in Python to work with data related to JSON. JSON is text and written in javascript format.

A Python object can be converted into the various types of Json string such as dict list, tuple, string, int, float, True, False, None.

Example – Convert Python object into Json string.

import json

x = {'name' :  'Alex', 'Add' : 'NYC', 'ID' : 123}
z = json.dumps(x) 

print(z)
python-json-method

Explanation – As shown clearly, it converted Python dict list into JSON string then returned whole dictionary.

a = json.dumps(['a' , 'b' , 'c'])
print(a)
python-json-method

Explanation – As shown clearly, it converted Python list string into JSON string then returned whole list string.

a = json.dumps((1 , 2 , 3))
print(a)
python-json-method

Explanation – As shown clearly, it converted Python tuple into JSON string then returned whole list.

a = json.dumps('Alex')
print(a)
python-json-method

Explanation – As shown clearly, that it converted Python tuple string into JSON string then returned string.

a = json.dumps(23)
print(a)
python-json-method

Explanation – As shown clearly, it converted Python tuple into JSON string then returned tuple.

a = json.dumps(23.3)
print(a)
python-json-method

Explanation – As shown clearly, it converted Python tuple into JSON string then returned tuple.

a = json.dumps(True)
print(a)
python-json-method

Explanation – As shown clearly, it converted Python Boolean into JSON string then returned Boolean value.

a = json.dumps(False)
print(a)
python-json-method

Explanation – As shown clearly, it converted Python Boolean into JSON string then returned Boolean value.

a = json.dumps(None)
print(a)
python-json-method

Explanation – As it is shown clearly that it converted Python keyword into JSON string then returned keyword.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets