How to Convert Python Objects Into JSON Strings


In this example, we will learn how to convert Python objects into JSON strings and print the values.

Source Code

import json

print(json.dumps(16))      # convert number into JSON strings
print(json.dumps('python'))    # convert string into JSON strings
print(json.dumps(['a', 'b', 'c']))    # convert list into JSON strings
print(json.dumps(('a', 'b', 'c')))    # convert tuple into JSON strings
print(json.dumps({'name': 'Jim', 'id': 15}))     # convert dict into JSON strings
print(json.dumps(True))     # convert True into JSON strings
print(json.dumps(None))     # convert None into JSON strings

Output:

16
"python"
["a", "b", "c"]
["a", "b", "c"]
{"name": "Jim", "id": 15}
true
null
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement