I have a json file which has 18 substrings like this: https://i.sstatic.net/aVWuw.png https://i.sstatic.net/RLlRX.png
But I have more json files who have different number of these substrings. So I did this to find how many there are in the text:
import jsonjson_str = open('jsonfile.txt', 'r').read()contact = json.loads(json_str)
So GraphImages_total is 18 . Every substring has comments --> data --> 0 --> owner --> username So I want to print the username.
comment_author = contact["GraphImages"][0]["comments"]["data"][0]["owner"]["username"]
print(comment_author)
This is for GraphImages_total = 0 But I want to do it for all of them.
So I need a way to make it like this:
for graph_image in contact['GraphImages']:comment_author = contact["GraphImages"][graph_image]["comments"]["data"][0]["owner"]["username"]print(comment_author)
But I get this error:
comment_author = contact["GraphImages"][graph_image]["comments"]["data"][0]["owner"]["username"]IndexError: list index out of range