I have a nested JSON object below.
var payload = {
"name" : {
"first" : "I am a",
"last" : "Customer"
},
"product_info": {
"name" : "Product 1",
"category" : {
"id" : "123456789",
"name" : "C1",
"sub_category" : {
"id" : "321654987",
"name" : "SC1"
},
}
}};
var param = JSON.stringify(payload);
How can I unnest the JSON object, so I can have the JSON object as below?
var result = {
"name.first" : "I am a",
"name.last" : "Customer",
"product_info.name" : "Product 1",
"product_info.category.id" : "123456789",
"product_info.category.name" : "C1",
"product_info.category.sub_category.id" : "321654987",
"product_info.category.sub_category.name" : "SC1"
}
I was trying to use the Object.keys(param).length
to check how many nested object there are, but if there isn't a nested object, it will return the length of the value.
Any suggestion would be appreciated.