Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Created March 10, 2017 03:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keithweaver/f803d6ad45d3ccc193315d8af40221be to your computer and use it in GitHub Desktop.
Save keithweaver/f803d6ad45d3ccc193315d8af40221be to your computer and use it in GitHub Desktop.
Get JSON file with Python
import json
def getJSON(filePathAndName):
with open(filePathAndName, 'r') as fp:
return json.load(fp)
# Example of returning just JSON
# jsonFile = getJSON('./test.json') # Uncomment this line
# It gets returned as a dictionary.
# Example of getting a list from a JSON file
# test.json looks like:
# {
# "photos" : [
# "test1",
# "test2"
# ]
# }
listOfPhotos = getJSON('./test.json').get('photos',[])
# If 'photos' is not in the JSON file then it would set it to []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment