def hierarchy(struct, path=None):
if isinstance(struct, dict):
path = path if path else '$'
return set(
child_path
for key, obj in struct.items()
for child_path in hierarchy(obj, f'{path}.{key}')
).union(
[path]
)
elif isinstance(struct, list):
path = f'{path}[]' if path else '$[]'
return set(
child_path
for obj in struct
for child_path in hierarchy(obj, path)
).union(
[path]
)
else:
return [path]
from itertools import chain
def hierarchy2(d):
if isinstance(d, dict):
return set(
f'{k}.{x}' if x else k
for k,v in d.items()
for x in chain([''], hierarchy2(v))
)
elif isinstance(d, list):
return set(
v
for l in d
for v in hierarchy2(l)
if v
)
else:
return set()
inpt = {
'0': [
['abc', {'0a': 'test'}],
['def', {'0a': 'test'}],
['ghi', {'0b': 'test'}]
],
'1': {
'2': 'test',
'3': {
'4': 'test',
'5': 'test'
},
},
'6': [
'test',
{
'7': 'test'
},
[
{
'8': 'test'
},
{
'9': 'test'
},
]
]
}
print('Example1\n')
out = sorted(hierarchy(inpt))
print(out)
print()
out = sorted(hierarchy2(inpt))
print(out)
print()
print()
inpt = [ 'test', {'test': 'test'}, [ {'test': 'test'} ] ]
print('Example2\n')
out = sorted(hierarchy(inpt))
print(out)
print()
out = sorted(hierarchy2(inpt))
print(out)
print()
print()
inpt={'boo': 'its', 'soo': 'your', 'roo': 'choice', 'qoo': 'this', 'fizz': 'is', 'buzz': 'very', 'yoyo': 'rambling', 'wazzw': 'lorem', 'bnn': 'ipsum', 'cc': [{'boo': 'fill', 'soo': 'ing', 'roo': 'in', 'qoo': 'the', 'fizz': 'words', 'buzz': 'here', 'yoyo': 'we', 'wazzw': 'go', 'nummm': 2, 'bsdfff': 3, 'hgdjgkk': 4, 'opu': 1, 'mnb': True}, {'boo': 'again', 'soo': 'loop', 'roo': 'de', 'qoo': 'loop', 'fizz': 'wowzers', 'buzz': 'try', 'yoyo': 'again', 'wazzw': 'how', 'nummm': 1, 'bsdfff': 7, 'hgdjgkk': 0, 'opu': 1, 'mnb': True}], 'soos': ['ya'], 'tyu': 'doin', 'dddd3': 'today'}
print('Example3\n')
out = sorted(hierarchy(inpt))
print(out)
print()
out = sorted(hierarchy2(inpt))
print(out)
print()
print()
inpt = {'l1': {'l2': {'l3': 'test'}}}
print('Example4\n')
out = sorted(hierarchy(inpt))
print(out)
print()
out = sorted(hierarchy2(inpt))
print(out)
print()
print()
inpt = [{'a': 0}, {'a': 1}, {'b': 0}]
print('Example5\n')
out = sorted(hierarchy(inpt))
print(out)
print()
out = sorted(hierarchy2(inpt))
print(out)
print()
print()
To embed this project on your website, copy the following code and paste it into your website's HTML: