From the following json, in python, I'd like to extract the value "TEXT". All the keys are constant except for unknown. Unknown could be any string like "a6784t66" or "hobvp*nfe". The value of unknown is not known, only that it will be in that position in each json response.
{
"A": {
"B": {
"unknown": {
"1": "F",
"maindata": [
{
"Info": "TEXT"
}
]
}
}
}
}
one line json
'{"A":{"B":{"unknown":{"1":"F","maindata":[{"Info":"TEXT"}]}}}}'
How would you get the value of "Text"? (I know how to load the json with json.loads)..but I'm not sure how to get the value of "Text". Thanks.
(I'm not sure what the best title is.)
You can use a recursive function to dig through every layer and print its value with an indent
def recurse_keys(df, indent = ' '):
'''
import json, requests, pandas
r = requests.post(...)
rj = r.json() # json decode results query
j = json.dumps(rj, sort_keys=True,indent=2)
df1 = pandas.read_json(j)
'''
for key in df.keys():
print(indent+str(key))
if isinstance(df[key], dict):
recurse_keys(df[key], indent+' ')
recurse_keys(df1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With