I have a payload structure to add datas into db, each object have name with child array, db should add each object with parent id. how to handle these array in loop adding data in database?
payload structure:
{
"name": "food",
"child_categories": [
{
"name": "non-frozen",
"child_categories": [
{
"name":"non-branded",
"child_categories": [
{
"name":"food-grains",
"child_categories": [
{
"name":"rice"
},
{
"name": "atta"
},
{
"name": "maida"
},
{
"name": "sooji"
},
{
"name":"ragi"
},
{
"name": "jower"
},
{
"name": "bajra"
}
]
},
{
"name":"salt&spices",
"child_categories": [
{
"name":"whole-spices"
},
{
"name":"grounded-spices"
}
]
},
{
"name":"dried-condiments",
"child_categories": [
{
"name":"salt"
},
{
"name":"powders"
},
{
"name":"sugar"
},
{
"name":"jaggery"
}
]
},
{
"name": "edible-oil&ghee"
},
{
"name": "lentils",
"child_categories":[
{
"name":"all-dals"
},
{
"name":"all-rajma"
},
{
"name":"chanas"
},
{
"name":"millets"
},
{
"name":"soya"
}
]
}
]
},
{
"name":"branded",
"child_categories": [
{
"name":"food-grains",
"child_categories": [
{
"name":"rice"
},
{
"name": "atta"
},
{
"name": "maida"
},
{
"name": "sooji"
},
{
"name":"ragi"
},
{
"name": "jower"
},
{
"name": "bajra"
}
]
},
{
"name":"salt&spices",
"child_categories": [
{
"name":"whole-spices"
},
{
"name":"grounded-spices"
}
]
},
{
"name":"dried-condiments",
"child_categories": [
{
"name":"salt"
},
{
"name":"powders"
},
{
"name":"sugar"
},
{
"name":"jaggery"
}
]
},
{
"name": "edible-oil&ghee"
},
{
"name": "lentils",
"child_categories":[
{
"name":"all-dals"
},
{
"name":"all-rajma"
},
{
"name":"chanas"
},
{
"name":"millets"
},
{
"name":"soya"
}
]
},
{
"name":"snacks&cereals",
"child_categories":[
{
"name":"biscuits"
},
{
"name":"cookies"
},
{
"name":"bhujia"
},
{
"name":"mixture"
}
]
},
{
"name":"packaged-raw",
"child_categories":[
{
"name":"noodles"
},
{
"name":"pasta"
},
{
"name":"macaroni"
},
{
"name":"spreads"
},
{
"name":"sauces"
},
{
"name":"ready-to-eat"
},
{
"name":"chocs"
},
{
"name":"pickles&chutney"
}
]
}
]
}
]
}
]
}
Each categories have sub categories,with up level parent_id, once first level category added, it should take that id, add it into child category. example structure below
{
_id: "1",
parent_category_id: null, // 1st level category
name:"food"
}
{
_id:2,
parent_category_id: 1,
name: "non-frozen"
}
{
_id: 3,
parent_category_id: 2,
name: "non-branded"
}
{
_id: 4,
parent_category_id: 3,
name: "food-grains"
}
and so on...