I need to extract a single float from 100s or 1000s of .json files (which I am not familiar with, and do not have control over their creation), and use them later in my code. Relevant excerpt with shortened names:
...
"a": {
"b": {
"variable_name": {
"known_key": 133.2982,
...
There are multiple additional keys at the same level of "a", "b", and "known_key". I have no way of knowing what "variable_name" will be before accessing the file and do not need to track it in anyway. I do know that it will be the only key at that level of the dictionary and that it is almost guaranteed to not be unique among the different .json files.
using this answer I was able to determine that I could generically access the "variable_name" key by repeating the entire dictionary structure to that point and using .keys()[0]
but it feels like there should be a better way of doing this?
with open("json_file_X.json", "r") as j_in:
data = json.load(j_in)
needed = data["a"]["b"][list(data["a"]["b"].keys())[0]]["known_key"]
#do downstream stuff with needed float value after closing .json file
I know that I could substitute the following 2 line for loop for the 'needed' line above, but this seems wrong because someone else looking at this code would think I'm iterating over all the keys and only keeping the last value.
for var_key in data["a"]["b"]:
needed = data["a"]["b"][var_key]["known_key"]
So that leaves me specifically interested in a way to simplify [list(data["a"]["b"].keys())[0]]
given that I know there is only 1 key at that level or wondering if i'm going about the .json file structure completely wrong given that I only need 1 value out of the entire file.