0

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!

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.