Created
March 10, 2017 03:47
-
-
Save keithweaver/f803d6ad45d3ccc193315d8af40221be to your computer and use it in GitHub Desktop.
Get JSON file with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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