From the course: Python: Working with Files

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Write data to a JSON file in Python

Write data to a JSON file in Python - Python Tutorial

From the course: Python: Working with Files

Write data to a JSON file in Python

- [Instructor] JSON is one of the most popular formats to serialize data. After creating dictionary data with Python, you may want to save it to a JSON file. Here we start off with some sample code that will convert a given set of monster attributes into a dictionary object. Each monster product has a name, title, price, and level of scariness. We can use this function to generate a dictionary for a given monster. Let's try it. Here we create Filo who is a baker by day and techie by night. It's priced at 29 and has a scariness level of three. Let's generate a few more items. To write these dictionary objects out to a JSON file. We can use the JSON module. We'll open up a new file in write mode, and then write out the dictionary data in a JSON format. We can use the dump function to write the data in the correct format. Let's call this function with a list of our dictionary items. That's monster one, two, and…

Contents