javascript to print a value from a nested array in json
could someone help me to parse the value of action.keyword from the below json. I was able to parse only till "hits".
const rsp = `{ "took": 10, "hits": { "total": { "value": 520 } }, "aggregations": { "2": { "buckets": [{ "1": { "hits": { "total": { "value": 2 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "Start" ] }, "key": "TEST2", "doc_count": 4566 }] } } }, { "1": { "hits": { "total": { "value": 10 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "End" ] }, "key": "TEST3", "doc_count": 454 }] } } } ] } } }`
const obj = JSON.parse(rsp);
const recEvents = obj['aggregations']['2']['buckets'];
const act_key = recEvents['1']['hits']['hits'];
console.log(act_key);
1 answer
-
answered 2020-11-25 07:08
Eugene Karataev
Like this
const act_key = recEvents[0]['1']['hits']['hits'][0]['fields']['action.keyword'];
const rsp = `{ "took": 10, "hits": { "total": { "value": 520 } }, "aggregations": { "2": { "buckets": [{ "1": { "hits": { "total": { "value": 2 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "Start" ] }, "key": "TEST2", "doc_count": 4566 }] } } }, { "1": { "hits": { "total": { "value": 10 }, "hits": [{ "type": "doc", "score": null, "fields": { "action.keyword": [ "End" ] }, "key": "TEST3", "doc_count": 454 }] } } } ] } } }` const obj = JSON.parse(rsp); const recEvents = obj['aggregations']['2']['buckets']; const act_key = recEvents[0]['1']['hits']['hits'][0]['fields']['action.keyword']; console.log(act_key);