I have a nested JSON array that getting from a mongoDB query that i would like to convert into flat JSON .I am using nested mondo documents, but i would like to show the data in a more readble way. My JSON has the following structure:
[{
"country": "Country A",
"regions": [{
"region": "region A1",
"cities": [{
"city": "city A11"
},
{
"city": "city A12"
}
]
},
{
"region": "region A2",
"cities": [{
"city": "city A21"
},
{
"city": "city A22"
}
]
}
]
},
{
"country": "Country B",
"regions": [{
"region": "region B1",
"cities": [{
"city": "city B11"
},
{
"city": "city B12"
}
]
},
{
"region": "region B2",
"cities": [{
"city": "city B21"
},
{
"city": "city B22"
}
]
}
]
}
]
I want to show only the important information and not the structure of the nested array. How i can modify my data in Javascript on order to achieve the following result.
[
{
"country": "Country A",
"region":"Region A1",
"city": "City A11"
},
{
"country": "Country A",
"region":"Region A1",
"city": "City A12"
},
-------------
{
"country": "Country B",
"region":"Region B1",
"city": "City B11"
},
-----------
{
"country": "Country B",
"region":"Region B2",
"city": "City B22"
}
]
What is the simpliest way to acheive this result?