analizar JSON sin saber el valor de la clave

Sé cómo analizar un JSON sabiendo el valor clave, pero ahora me gustaría obtener los valores clave de un JSON que no es mío, así que puedo saber los nombres de las claves, por ejemplo, tengo este JSON

[ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" } }, { "id": 2, "name": "Ervin Howell", "username": "Antonette", "email": "Shanna@melissa.tv", "address": { "street": "Victor Plains", "suite": "Suite 879", "city": "Wisokyburgh", "zipcode": "90566-7771", "geo": { "lat": "-43.9509", "lng": "-34.4618" } }, "phone": "010-692-6593 x09125", "website": "anastasia.net", "company": { "name": "Deckow-Crist", "catchPhrase": "Proactive didactic contingency", "bs": "synergize scalable supply-chains" } }, ... ] 

Así que a partir de ahora tengo esto:

 with open('users.json') as f: data = json.load(f) 

¿Dónde puedo ver todo el JSON cargado si imprimo data Entonces, mi pregunta es, ¿cómo imprimo todas las llaves y los objetos nesteds sin saber el nombre?

Mi objective es tener una dirección de correo electrónico de nombre de usuario que contenga calle, suite, ciudad, código postal, geo que contenga lat, long, etc.

Related of "analizar JSON sin saber el valor de la clave"

Aquí hay un generador recursivo que escaneará una estructura de diccionario / lista anidada, como la que se obtiene al cargar JSON en Python. Le muestra la secuencia de las claves de diccionario y los índices de lista asociados con cada valor.

He modificado ligeramente sus datos para ilustrar cómo maneja las listas anidadas dentro de los dictados.

 data = [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" }, "other": ["This", "is", "a list"] }, { "id": 2, "name": "Ervin Howell", "username": "Antonette", "email": "Shanna@melissa.tv", "address": { "street": "Victor Plains", "suite": "Suite 879", "city": "Wisokyburgh", "zipcode": "90566-7771", "geo": { "lat": "-43.9509", "lng": "-34.4618" } }, "phone": "010-692-6593 x09125", "website": "anastasia.net", "company": { "name": "Deckow-Crist", "catchPhrase": "Proactive didactic contingency", "bs": "synergize scalable supply-chains" }, "other": ["This", "is", "another list"] }, ] def show_indices(obj, indices): for k, v in obj.items() if isinstance(obj, dict) else enumerate(obj): if isinstance(v, (dict, list)): yield from show_indices(v, indices + [k]) else: yield indices + [k], v for keys, v in show_indices(data, []): print(keys, v) 

salida

 [0, 'id'] 1 [0, 'name'] Leanne Graham [0, 'username'] Bret [0, 'email'] Sincere@april.biz [0, 'address', 'street'] Kulas Light [0, 'address', 'suite'] Apt. 556 [0, 'address', 'city'] Gwenborough [0, 'address', 'zipcode'] 92998-3874 [0, 'address', 'geo', 'lat'] -37.3159 [0, 'address', 'geo', 'lng'] 81.1496 [0, 'phone'] 1-770-736-8031 x56442 [0, 'website'] hildegard.org [0, 'company', 'name'] Romaguera-Crona [0, 'company', 'catchPhrase'] Multi-layered client-server neural-net [0, 'company', 'bs'] harness real-time e-markets [0, 'other', 0] This [0, 'other', 1] is [0, 'other', 2] a list [1, 'id'] 2 [1, 'name'] Ervin Howell [1, 'username'] Antonette [1, 'email'] Shanna@melissa.tv [1, 'address', 'street'] Victor Plains [1, 'address', 'suite'] Suite 879 [1, 'address', 'city'] Wisokyburgh [1, 'address', 'zipcode'] 90566-7771 [1, 'address', 'geo', 'lat'] -43.9509 [1, 'address', 'geo', 'lng'] -34.4618 [1, 'phone'] 010-692-6593 x09125 [1, 'website'] anastasia.net [1, 'company', 'name'] Deckow-Crist [1, 'company', 'catchPhrase'] Proactive didactic contingency [1, 'company', 'bs'] synergize scalable supply-chains [1, 'other', 0] This [1, 'other', 1] is [1, 'other', 2] another list 

Puede utilizar estas listas para acceder a cualquier elemento, por ejemplo,

 keys = [1, 'company', 'catchPhrase'] obj = data for k in keys: obj = obj[k] print(obj) 

salida

 Proactive didactic contingency 

O si quieres modificar un artículo:

 keys = [1, 'company', 'catchPhrase'] obj = data for k in keys[:-1]: obj = obj[k] obj[keys[-1]] = "some new thing" print(data[1]['company']) 

salida

 {'name': 'Deckow-Crist', 'catchPhrase': 'some new thing', 'bs': 'synergize scalable supply-chains'} 

Prueba algo como esto

 for d in data: for key in d.keys(): print(d[key])