Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Writing to a JSON file
2. Convert a Python object into a JSON string
Summary

Instruction

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:

json.dumps(data)

Finally, we can print the result:

print(json.dumps(data))

Exercise

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?