I have strings that represent JSON key paths. I would like to use those to go get the actual value of that path inside of my nested JSON data, like so:
path_a = "['id']"
path_b = "['users'][0]['address']['street']"
path_c = "['houses'][2]['block'][0]['is_viable']"
print(json_data[path_a])
print(json_data[path_b])
print(json_data[path_c])
As you guessed, the print returns an error:
KeyError: "['id']"
Is there a way to achieve what I'm trying to do? I gave a few examples of paths here but the main thing about my code is that I cannot know in advance how many "keys" will be in a path, so I cannot simply type print(json_data[path[0]][path[1]][path[2]][path[3]][path[4]])
.