Skip to content

Instantly share code, notes, and snippets.

@ProbonoBonobo
Created October 2, 2021 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProbonoBonobo/8633da8007019d4fba6a44598124d3d0 to your computer and use it in GitHub Desktop.
Save ProbonoBonobo/8633da8007019d4fba6a44598124d3d0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# suppose we have a key:value mapping saved to "/tmp/my_obj.json". (Note: 'json' is a file serialization format that works in a similar way as Python `dict` objects)
# first, import the built-in `json` module
import json
with open("/tmp/my_obj.json", "r") as f: # open the file in "read" mode (indicated by the second argument, "r")...
my_obj = json.load(f) # and create a Python dictionary object `my_obj` from the key:value pairs in the file using the `json.load` function.
# you can preview the contents of my_obj by pretty-printing it like so:
print(json.dumps(my_obj, indent=4))
# you can also access its keys...
print(my_obj.keys())
# values...
for val in my_obj.values():
print(val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment