awdoibbbbbbb2
awdoibbbbbbb2

Reputation: 63

How to load data from multiple JSON files from a list using python?

Essentially, I have a list of JSON files that I need information extracted from which is saved in a variable called json_files.

The information from these files needs to then be aggregated and transferred into a new file called summary.json.

Currently, I am able to do this with the following lines of code but only for one file at a time by specifying an index:

with open(json_files[1]) as f:
    data = json.load(f)

with open('summary.json', 'w') as f:
    json.dump(data, f, indent=2)

However, if I try using a for loop like as follows, I get a TypeError: list indices must be integers or slicers, not str

for i in json_files:
    with open(json_files[i]) as f:
        data = json.load(f)

with open('summary.json', 'w') as f:
    json.dump(data, f, indent=2)

Thus, I was wondering what would be the best way to load information from multiple json files and then combine that into 1 new large json file?

Thanks in advance

Upvotes: 1

Views: 1408

Answers (1)

Audemed
Audemed

Reputation: 281

for i in json_files:
    with open(json_files[i]) as f:
        data = json.load(f)

Should be

for i in json_files:
    with open(i) as f:
        data = json.load(f)

What's happening: json_files is a list with names I'm assuming, so something like json_files = ['file1.json', 'file2.json']

So your open method was getting parameters like json_files['file1.json'] within the for loop which doesn't make sense since json_files is a list and not a dictionary.

The i in the for loop holds the actual value in the list, not the indexes.

Edit:

The data variable in the above code is being overwritten in each iteration of the loop! You probably need to do something like this to fix it:

combined = []
    for i in json_files:
        with open(i) as f:
            combined.append(json.load(f))

And then use the dump function to write this list into another file.

Upvotes: 3

Related Questions