Python json accessing an id in a nested dictionary and list based on it's value
I have a nested dictionary with lists that I'm trying to access the record in the dictionary that contains the value of my search_id variable.
Without knowing index number of the list how would I access the node with an id that ='C0.N.01.B'
I've taken only a single record from the nested dictionary to illustrate the issue, but there are thousands so I can't just point to the index or key/value without using the variable.
I've tried a lot more than the comments below, but I rewrote the code to be minimal and reproducible.
search_id = 'C0.N.01.B'
result = {'status': 'single', 'searchResult': {'id': 'C0.N.01.A', 'node': {'id': {'content': 'C0'}, 'label': {'content': 'Anatomical Therapeutic Chemical (ATC1-4)'}, 'sourceId': {'content': '0'}, 'numChildren': {'content': '14'}, 'numDrugs': {'content': '2755'}, 'node': [{'id': {'content': 'C0.A'}, 'label': {'content': 'ALIMENTARY TRACT AND METABOLISM'}, 'sourceId': {'content': 'A'}, 'numChildren': {'content': '16'}, 'numDrugs': {'content': '397'}, 'node': []}, {'id': {'content': 'C0.B'}, 'label': {'content': 'BLOOD AND BLOOD FORMING ORGANS'}, 'sourceId': {'content': 'B'}, 'numChildren': {'content': '5'}, 'numDrugs': {'content': '158'}, 'node': []}, {'id': {'content': 'C0.C'}, 'label': {'content': 'CARDIOVASCULAR SYSTEM'}, 'sourceId': {'content': 'C'}, 'numChildren': {'content': '9'}, 'numDrugs': {'content': '326'}, 'node': []}, {'id': {'content': 'C0.D'}, 'label': {'content': 'DERMATOLOGICALS'}, 'sourceId': {'content': 'D'}, 'numChildren': {'content': '11'}, 'numDrugs': {'content': '242'}, 'node': []}, {'id':
{'content': 'C0.G'}, 'label': {'content': 'GENITO URINARY SYSTEM AND SEX HORMONES'}, 'sourceId': {'content': 'G'}, 'numChildren': {'content': '4'}, 'numDrugs': {'content': '160'}, 'node': []}, {'id': {'content': 'C0.H'}, 'label': {'content': 'SYSTEMIC HORMONAL PREPARATIONS, EXCL. SEX HORMONES AND INSULINS'}, 'sourceId': {'content': 'H'}, 'numChildren': {'content': '5'}, 'numDrugs': {'content': '66'}, 'node': []}, {'id': {'content': 'C0.J'}, 'label': {'content': 'ANTIINFECTIVES FOR SYSTEMIC USE'}, 'sourceId': {'content': 'J'}, 'numChildren': {'content': '6'}, 'numDrugs': {'content': '334'}, 'node': []}, {'id': {'content': 'C0.L'}, 'label': {'content': 'ANTINEOPLASTIC AND IMMUNOMODULATING AGENTS'}, 'sourceId': {'content': 'L'}, 'numChildren': {'content': '4'}, 'numDrugs': {'content': '324'}, 'node': []}, {'id': {'content': 'C0.M'}, 'label': {'content': 'MUSCULO-SKELETAL SYSTEM'}, 'sourceId': {'content': 'M'}, 'numChildren': {'content': '6'}, 'numDrugs': {'content': '130'}, 'node': []}, {'id': {'content': 'C0.N'}, 'label': {'content': 'NERVOUS SYSTEM'}, 'sourceId': {'content': 'N'}, 'numChildren': {'content': '7'}, 'numDrugs': {'content': '433'}, 'node': [{'id': {'content': 'C0.N.01'}, 'label': {'content': 'ANESTHETICS'}, 'sourceId': {'content': 'N01'}, 'numChildren': {'content': '2'}, 'numDrugs': {'content': '45'}, 'node': [{'id': {'content': 'C0.N.01.A'}, 'label': {'content': 'ANESTHETICS, GENERAL'}, 'sourceId':
{'content': 'N01A'}, 'numChildren': {'content': '6'}, 'numDrugs': {'content': '26'}, 'node': []}, {'id': {'content': 'C0.N.01.B'}, 'label': {'content': 'ANESTHETICS, LOCAL'}, 'sourceId': {'content': 'N01B'}, 'numChildren': {'content': '4'}, 'numDrugs': {'content': '19'}, 'node': []}]}, {'id': {'content': 'C0.N.02'}, 'label': {'content': 'ANALGESICS'}, 'sourceId': {'content': 'N02'}, 'numChildren': {'content': '3'}, 'numDrugs': {'content': '70'}, 'node': []}, {'id': {'content': 'C0.N.03'}, 'label': {'content': 'ANTIEPILEPTICS'}, 'sourceId': {'content': 'N03'}, 'numChildren': {'content': '1'}, 'numDrugs': {'content': '43'}, 'node': []}, {'id': {'content': 'C0.N.04'}, 'label': {'content': 'ANTI-PARKINSON DRUGS'}, 'sourceId': {'content': 'N04'}, 'numChildren': {'content': '3'}, 'numDrugs': {'content': '28'}, 'node': []}, {'id': {'content': 'C0.N.05'}, 'label': {'content': 'PSYCHOLEPTICS'}, 'sourceId': {'content': 'N05'}, 'numChildren': {'content': '3'}, 'numDrugs': {'content': '131'}, 'node': []}, {'id': {'content': 'C0.N.06'}, 'label': {'content': 'PSYCHOANALEPTICS'}, 'sourceId': {'content': 'N06'}, 'numChildren': {'content': '4'}, 'numDrugs': {'content': '87'}, 'node': []}, {'id': {'content': 'C0.N.07'}, 'label': {'content': 'OTHER NERVOUS SYSTEM DRUGS'}, 'sourceId': {'content': 'N07'}, 'numChildren': {'content': '4'}, 'numDrugs': {'content': '37'}, 'node': []}]}, {'id': {'content': 'C0.P'}, 'label': {'content': 'ANTIPARASITIC PRODUCTS, INSECTICIDES AND REPELLENTS'}, 'sourceId': {'content': 'P'}, 'numChildren': {'content': '3'}, 'numDrugs': {'content': '77'}, 'node': []}, {'id': {'content': 'C0.R'}, 'label': {'content': 'RESPIRATORY SYSTEM'}, 'sourceId': {'content': 'R'}, 'numChildren': {'content': '6'}, 'numDrugs': {'content': '213'}, 'node': []}, {'id': {'content': 'C0.S'}, 'label': {'content': 'SENSORY ORGANS'},
'sourceId': {'content': 'S'}, 'numChildren': {'content': '3'}, 'numDrugs': {'content': '174'}, 'node': []}, {'id': {'content': 'C0.V'}, 'label': {'content': 'VARIOUS'}, 'sourceId': {'content': 'V'}, 'numChildren': {'content': '9'}, 'numDrugs': {'content': '137'}, 'node': []}]}}, 'spellingResult': None, 'relatedTree': None, 'tableResult': None, 'synResult': None}
node = result['searchResult']['node'] # prints the dictionary
print(f'node: {node}')
id = result['searchResult']['node']['id'] # prints the first records key/value
print(f'id: {id}')
#id = result['searchResult']['node']['id']['C0.N.01.B'] # gives key error
#print(f'id: {id}')
#id = result['searchResult']['node']['id'].values({search_id}) # gives values takes no argument 1 given error
#print(f'id: {id}')
for search_id in result['searchResult']['node']:
node_id = result['searchResult']['node']
print(f'node id: {node_id}')
search_node_id = result['searchResult']['node']['id']['content']
print(f'search node id: {search_node_id}')
print(type(search_node_id))
number_of_child = result['searchResult']['node']['numChildren']['content']
print(f'number_of_child: {number_of_child}')
print(type(number_of_child))
number_of_drugs = result['searchResult']['node']['numDrugs']['content']
print(f'number_of_drugs: {number_of_drugs}')
print(type(number_of_drugs))
class_source_id = result['searchResult']['node']['sourceId']['content']
print(f'class_source_id: {class_source_id}')
print(type(class_source_id))
This is the record of the dictionary I'd like to access. How do I go about only accessing this record where the search_id = id of the node?
'node': [{'id': {'content': 'C0.N.01.A'}, 'label': {'content': 'ANESTHETICS, GENERAL'}, 'sourceId': {'content': 'N01A'}, 'numChildren': {'content': '6'}, 'numDrugs': {'content': '26'}
You could use a recursive function to search the dicts in a node, if the search is in this node then return it, otherwise if this node has more nodes search them.
def search_nodes(nodes: list, search_content: str) -> dict:
for node in nodes:
if node['id']['content'] == search_content:
return node
if node['node']:
response = search_nodes(node['node'], search_content)
if response:
return response
nodes = result['searchResult']['node']['node'] # gets the list of nodes
searched_node = search_nodes(nodes, search_id)
print(searched_node)
OUTPUT
{'id': {'content': 'C0.N.01.B'}, 'label': {'content': 'ANESTHETICS, LOCAL'}, 'sourceId': {'content': 'N01B'}, 'numChildren': {'content': '4'}, 'numDrugs': {'content': '19'}, 'node': []}