Snatch any course you want for just $15! Go to “Courses” and find a course ↓
Writing to a JSON file
5. Problems using json.dumps()


Well done! You may be wondering whether every type of Python object can be saved as a JSON object. Unfortunately, no. There are many objects that can be converted to JSON; we've listed a few below with their JSON equivalents:

Python JSON
dict object
list array
str string
int long number
float number
True true
False false
None null

Sometimes, we can get into trouble when converting an object. For example, we could have the following problem if we tried to convert a Python set.


Create a set named random that contains the values 3.14, 273, and 'a'. Then, call the json.dumps() function with this object as an argument.
Will we get an error? If so, what kind of error?

Stuck? Here's a hint!

Create a random set:

random = {3.14, 273, 'a'}

Then, use json.dumps(random).