0

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"
 }
 ]
 }
]
oniramarf
9141 gold badge12 silver badges30 bronze badges
asked Feb 18, 2018 at 22:50

3 Answers 3

4

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);
 }
});
answered Feb 18, 2018 at 23:19
Sign up to request clarification or add additional context in comments.

Comments

0

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);

answered Feb 19, 2018 at 7:47

Comments

0

Here's a single line solution that iterates on the response JSON Array

resXX.forEach(item => item.msr.forEach(v => v['key']==='duplicated_lines' && console.log(`Checking the id ${v['val']}`)))
answered Feb 20, 2018 at 6:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.