I have a scenario where I am trying to parse through this below json and get all the value of key "name" and key "id" , which I would further store in a variable or array.
[
{
"metadata": {
"id": "vvvvvvvvvvvvv",
"url": "cccccccccccccc",
"created_at": "2019-09-06T08:40:41Z",
"updated_at": "2019-09-06T13:25:46Z"
},
"entity": {
"name": "app1",
"b_enabled": false,
"d_url": "xxxxxs"
}
},
{
"metadata": {
"id": "vvvvvvvvvvccc",
"url": "cccccccccccccc",
"created_at": "2019-09-06T08:40:41Z",
"updated_at": "2019-09-06T13:25:46Z"
},
"entity": {
"name": "app2",
"b_enabled": false,
"d_url": "xxxxxs"
}
},
{
"metadata": {
"id": "vvvvvvvvvvddd",
"url": "cccccccccccccc",
"created_at": "2019-09-06T08:40:41Z",
"updated_at": "2019-09-06T13:25:46Z"
},
"entity": {
"name": "app3",
"b_enabled": false,
"d_url": "xxxxxs"
}
},
{
"metadata": {
"id": "vvvvvvvvvveee",
"url": "cccccccccccccc",
"created_at": "2019-09-06T08:40:41Z",
"updated_at": "2019-09-06T13:25:46Z"
},
"entity": {
"name": "app4",
"b_enabled": false,
"d_url": "xxxxxs"
}
},
{
"metadata": {
"id": "vvvvvvvvvvfff",
"url": "cccccccccccccc",
"created_at": "2019-09-06T08:40:41Z",
"updated_at": "2019-09-06T13:25:46Z"
},
"entity": {
"name": "app5",
"b_enabled": false,
"d_url": "xxxxxs"
}
}
]
What I have tried till now( after this not sure how to proceed as this itself provides wrong result)
const dJSON = require('dirty-json');
const jsn = dJSON.parse(get_all)
console.log(JSON.stringify(jsn));
jsonData = JSON.stringify(jsn)
console.log(jsonData.length) //this returns wrong value
for(var i = 0; i < jsonData.length; i++){
for(key in jsonData[i].entity){
if(jsonData[i].entity[key] == "name"){
return console.log(key);
}
}
}
This doesn't returns the expected output. Can someone advice here as I am new to nodejs & javascript on how can I extract result like this
expected o/p :
{
"app1" : "vvvvvvvvvvvvv",
"app2" : "vvvvvvvvvvccc",
"app3" : "vvvvvvvvvvddd",
"app4" : "vvvvvvvvvveee",
"app5" : "vvvvvvvvvvfff"
}