Reputation: 833
I have a JSON response that I'm looking to parse through. I'd like to get all of the keys that have a dictionary of values and are not just a string.
JSON:
[
{
"style": 7,
"accessDate": "2017-06-16T12:52:18Z",
"name": "Starbucks",
"location": {
"latitude": 40.767372,
"longitude": -111.891367,
},
"abstract": {
"type": 5
}
}
]
Basically I'd like to get a list of every key that has a dictionary (in this case, location
and abstract
). I tried using type()
but this just returns a string because...obviously.
for key in myjson:
print(type(key))
Is there an alternative to finding where these might exist within a json?
Upvotes: 1
Views: 118
Reputation: 82765
Use isinstance
Ex:
j = [
{
"style": 7,
"accessDate": "2017-06-16T12:52:18Z",
"name": "Starbucks",
"location": {
"latitude": 40.767372,
"longitude": -111.891367,
},
"abstract": {
"type": 5
}
}
]
for i in j:
for key, value in i.items():
if isinstance(value, dict):
print(key)
Output:
location
abstract
Upvotes: 1
Reputation: 9094
You can use isinstance
to see if a value is an instance of a class
for k, v in d.items():
if isinstance(v, dict):
print(k)
Upvotes: 1