Snatch any course you want for just $15! Go to “Courses” and find a course ↓
Writing to a JSON file
2. Convert a Python object into a JSON string


Great! First, we'll learn how to convert a Python object into a JSON string.

Take a Python object like this:

data = {
  'first_name': 'Guido',
  'second_name': 'van Rossum',
  'titles': ['BDFL', 'Developer']

You can use the json.dumps() function to produce a string representing a JSON object. The name of the function means dump the data as a string, note that the final "s" in "dumps" stands for "string". The function requires an argument denoting the object you'd like to dump:


Finally, we can print the result:



We have saved some information in a Python object named data. Using json.dumps(), convert this object to a string and print the result. Is the output readable?