I need to create a object from a json structure. I am confused on how to do the recursive of the structure.
This is my sample json structure
{
"dynamic.title": "Lorem Ipsum title",
"dynamic.main_section.header.title": "Lorem Ipsum main section header title ",
"dynamic.main_section.body.collection.first.title": "Lorem Ipsum main section body collection first title",
"dynamic.main_section.body.collection.first.description": "Lorem Ipsum main section body collection first description",
"dynamic.main_section.body.collection.first.id": "Lorem Ipsum main section body collection first id",
"dynamic.main_section.body.collection.first.child.first.title": "Lorem Ipsum main section body collection first of first child title ",
"dynamic.main_section.body.collection.first.child.first.id": "Lorem Ipsum main section body collection first of first child id ",
"dynamic.main_section.body.collection.first.child.first.url": "https://loremipsum/1",
"dynamic.main_section.body.collection.first.child.first.names.first": "John Doe",
"dynamic.main_section.body.collection.first.child.first.names.second": "Jane Doe",
"dynamic.main_section.body.collection.second.title": "Lorem Ipsum main section body collection second title",
"dynamic.main_section.body.collection.second.description": "Lorem Ipsum main section body collection second description",
"dynamic.main_section.body.collection.second.id": "Lorem Ipsum main section body collection second id",
"dynamic.main_section.body.collection.second.child.first.title": "Lorem Ipsum main section body collection second of first child title ",
"dynamic.main_section.body.collection.second.child.first.id": "Lorem Ipsum main section body collection second of first child id ",
"dynamic.main_section.body.collection.second.child.first.url": "https://loremipsum/2",
"dynamic.main_section.body.collection.second.child.second.title": "Lorem Ipsum main section body collection second of second child title ",
"dynamic.main_section.body.collection.second.child.second.id": "Lorem Ipsum main section body collection second of second child id ",
"dynamic.main_section.body.collection.second.child.second.url": "https://loremipsum/3"
}
What i required format is something like this
{
"dynamic": {
"title": "Lorem Ipsum title",
"main_section": {
"header": {
"title": "Lorem Ipsum main section header title "
},
"body": {
"collection": [
{
"title": "Lorem Ipsum main section body collection first title",
"description": "Lorem Ipsum main section body collection first description",
"id": "Lorem Ipsum main section body collection first id",
"child": [
{
"title": "Lorem Ipsum main section body collection first of first child title ",
"id": "Lorem Ipsum main section body collection first of first child id ",
"url": "https://loremipsum/1",
"names": ["John Doe", "Jane Doe"]
}
]
},
{
"title": "Lorem Ipsum main section body collection second title",
"description": "Lorem Ipsum main section body collection second description",
"id": "Lorem Ipsum main section body collection second id",
"child": [
{
"title": "Lorem Ipsum main section body collection second of first child title ",
"id": "Lorem Ipsum main section body collection second of first child id ",
"url": "https://loremipsum/2"
},
{
"title": "Lorem Ipsum main section body collection second of second child title ",
"id": "Lorem Ipsum main section body collection second of second child id ",
"url": "https://loremipsum/3"
}
]
}
]
}
}
}
}
If the left side key consist of first second then it will be an array, but if more than one property exist then it will be an object or else it could be an array.
How could i achieve this in js?