Skip to content
Snippets Groups Projects
Commit cf827783 authored by Richter, Manuela's avatar Richter, Manuela
Browse files

added recursive function for finding values in nested dictionaries

parent 31a6f6fa
No related branches found
No related tags found
No related merge requests found
......@@ -7,13 +7,43 @@ Created on Thu May 5 17:27:38 2022
testfile for operating with json-files
"""
#%% import moduls
import json
#%% define functions
def findkeys(node, kv):
'''
https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists
Parameters
----------
node : TYPE
DESCRIPTION.
kv : TYPE
DESCRIPTION.
Yields
------
TYPE
DESCRIPTION.
'''
if isinstance(node, list):
for i in node:
for x in findkeys(i, kv):
yield x
elif isinstance(node, dict):
if kv in node:
yield node[kv]
for j in node.values():
for x in findkeys(j, kv):
yield x
#%% main script
with open("test_data.json","r+") as file:
data = json.load(file)
print(len(data))
print(type(data))
#print(len(data))
#print(type(data))
#print(data["Name"])
......@@ -32,3 +62,7 @@ for i in a:
print(type(attrs))
print(attrs)
print(j)
print(list(findkeys(data, 'KPI')))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment