oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Writing a JSON File #364

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

Naturally we can use the json library to translate Python objects to JSON as well. This is especially useful in instances where you are using a Python library to serve web pages, you would also be able to serve JSON. Let's say we have a Python dictionary we wanted to save as a JSON file:

oldoc63 commented 1 year ago

We import the json module, open up a write-mode file under the variable json_file, and then use the json.dump() method to write to the file. json.dump() takes two arguments: first the data object, then the file object you want to save.