Reputation: 87
I have a directory full of json files all formatted the same as .json files. I am looping through each file in the directory to access the json data, but when I try to load the data I get an error.
for json_file in file_list:
with open(backup_folder.joinpath(json_file), 'r') as f:
json_loaded_data = json_file.load(f)
Running this code block returns:
AttributeError Traceback (most recent call last)
in
1 for json_file in file_list:
2 with open(backup_folder.joinpath(json_file), 'r') as f:
----> 3 json_loaded_data = json_file.load(f)
4
5 date = json_loaded_data['tradeDate']
AttributeError: 'str' object has no attribute 'load'
Example json file looks like this:
{
"settlements": [
{
"month": "MAR 19",
"open": "2.650",
"high": "2.744",
"low": "2.638",
"last": "2.647",
"change": "+.059",
"settle": "2.642",
"volume": "225,563",
"openInterest": "250,706"
}
}
Any help would be appreciated!
Upvotes: 0
Views: 174
Reputation: 897
json_file
is just a string containing the name of the file. Try this instead:
with open(backup_folder.joinpath(json_file), 'r') as f:
json_loaded_data = json.load(f)
Upvotes: 1