Skip to content

Instantly share code, notes, and snippets.

@dominiksimgen
Created February 5, 2021 15:20
Show Gist options
  • Save dominiksimgen/a6c49a57559ee17c7ffcf04ce9d9fc64 to your computer and use it in GitHub Desktop.
Save dominiksimgen/a6c49a57559ee17c7ffcf04ce9d9fc64 to your computer and use it in GitHub Desktop.
how to read and write to JSON files with Python
import json, os
here = os.path.dirname(os.path.abspath(__file__))
os.chdir(here)
f = open("database/locations.json", "r") # open the JSON with read access
data = json.load(f)
f.close()
#do here something with the "data" object like reading and manipulating data
f = open("database/locations.json", "w") # open the JSON with write access
json.dump(data, f) # dump the "data" object to the JSON
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment