Nested dictionary to database tables with sqlalchemy

Viewed 890

Assuming a database with tables G, P, and C. A one-to-many relationship exists between G and P, as well between P and C.

Having now a nested dictionary:

db_entry = {
    "G_key_1": {
        "G_col_1": 11,
        "G_col_2": 12,
        "G_Ps": [
            {
                "P_col_1": 21,
                "P_col_2": 22,
                "P_Cs": [
                    {
                        "C_col_1": 31,
                        "C_col_2": 32,
                    }
                ]
            }
        ]
    }
}

Is there a nice way to add the db_entry nested dictionary to the DB? At the moment I just loop over all the children since there are only 3 levels in the dictionary. But for dictionaries with more than 3 levels it would not be possible any longer.

0 Answers
Related