Skip to content
Advertisement

json list to nested based on id

How would I group JSON objects by id’s to make a nested JSON in Python? The JSON is structured this way:

JavaScript

The goal is to have a tree structure where each object(section) contains all its subsections going from 4 to 4.1 > 4.1.1 > 4.1.1.1.
The snippet of the required output is below:

JavaScript

Advertisement

Answer

You can use recursion:

JavaScript

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement