Need a recommendation on the compilation of the following algorithm:
There is a situation in which there may be a completely different JSON format. At one point in time, it can be:
{"root": { "child": "test_txt" } }
At another point in time it may be:
{"root": { "child_one": { "child_of_childOne": { "attr1":"test_txt" } }, "child_two": { "child_of_childTwo": { "attr2":"test_txt" } }, "child_three": { "child1_of_childThree": { "attr3":"test_txt" } "child2_of_childThree": { "attr4": { "the_deepest_attr": "test_txt" } } }, };
and even more difficult and even more nested may be at another point in time.
What is necessary? - you need to write a function that will iterate through all branches to the deepest attribute (to string "text_txt") and save the name of the deepest attribute with its value (as if you would need to get all attr from the example above)
Everything would be simple if there was only one branch, but there may be many branches with different levels of nesting. In addition, the names of the keys in this JSON are unknown, so you’ll not find much use for find.
My line of thought is this: take each object and recursively bypass it until I meet an attribute with a string. If an object has several "children" - remember it, saving it in an array. As you go around the "children" - remove them from the array.
But I'm not sure that this is the right approach, I decided to consult. Maybe someone has a ready-made solution. Thank you in advance.
root
, read the nesting level of each descendant with the memorization of depth, the key and the value of the deepest. If the next ones go deeper, change to new "leaders." No - go on. - user207618