简体   繁体   中英

Node.js JSON nested array object access

I am building an application which GETS JSON response from an API call in SONARQUBE.

Using node js how do I access the value of duplicated_lines from the below JSON object.

I tried the below but always get undefined. Is there any library that supports exploring these objects?

Code

var subrequest = unirest("GET",queryUrl);

subrequest.end(function (resXX) {
  if (resXX.error);
    for (var key in resXX) { 
      if (resXX.hasOwnProperty(key)) {
        console.log("Checking the id " + resXX[key].msr.duplicated_lines);
      }
    }
});

JSON

[
  {
    "id": 1933853,
    "uuid": "XXXXXXXXXXXXXX",
    "key": "XXXXXXXXXXXX",
    "name": "XXXXXXX",
    "scope": "PRJ",
    "qualifier": "TRK",
    "date": "2018-02-16T08:07:55-0500",
    "creationDate": "2017-09-20T09:50:05-0400",
    "lname": "XXXXXXXX",
    "version": "15",
    "msr": [
      {
        "key": "duplicated_lines",
        "val": 926192,
        "frmt_val": "926,192"
      },
      {
        "key": "bugs",
        "val": 7467,
        "frmt_val": "7,467"
      },
      {
        "key": "ncloc",
        "val": 1369829,
        "frmt_val": "1,369,829"
      },
      {
        "key": "code_smells",
        "val": 22677,
        "frmt_val": "22,677"
      },
      {
        "key": "vulnerabilities",
        "val": 95,
        "frmt_val": "95"
      }
    ]
  }
]

I don't know anything about the API you are accessing, and your code is hard to interpret in the absence of any other information. However msr is an array, so you need to access the objects in it with an index:

resXX[key].msr.forEach(m => {
    if (m.key === 'duplicated_lines') {
        console.log('Checking the id ' + m.val);
    }
});

I tried the below but always get undefined

Because you are trying to use object value as key while accessing.

Try this single line of code :

var duplicateData = jsonObj[0].msr.filter(item => item.key == 'duplicated_lines');

Working Demo

 var jsonObj = [ { "id": 1933853, "uuid": "XXXXXXXXXXXXXX", "key": "XXXXXXXXXXXX", "name": "XXXXXXX", "scope": "PRJ", "qualifier": "TRK", "date": "2018-02-16T08:07:55-0500", "creationDate": "2017-09-20T09:50:05-0400", "lname": "XXXXXXXX", "version": "15", "msr": [ { "key": "duplicated_lines", "val": 926192, "frmt_val": "926,192" }, { "key": "bugs", "val": 7467, "frmt_val": "7,467" }, { "key": "ncloc", "val": 1369829, "frmt_val": "1,369,829" }, { "key": "code_smells", "val": 22677, "frmt_val": "22,677" }, { "key": "vulnerabilities", "val": 95, "frmt_val": "95" } ] } ]; var duplicateData = jsonObj[0].msr.filter(item => item.key == 'duplicated_lines'); console.log(duplicateData); 

这是迭代响应JSON数组的单行解决方案

resXX.forEach(item => item.msr.forEach(v => v['key']==='duplicated_lines' && console.log(`Checking the id ${v['val']}`)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM