Sohan Sohan - 11 months ago 45
JSON Question

Parse json response in nodejs

I am beginner and learning javascript and never came across parsing json array. I have following string


I tried following,

//using following middle-ware as well
let app = express();
const collectioName = "credentials";
app.use(express.static(__dirname + '/public'));

//i get 'result' from one of query from mongodb
let response = JSON.parse(result);
console.log("got res " +response.credentials);

I also tried to get data in following way

for (var i=0; i<response['credentials'].length; i++){
console.log("got res " +response['credentials'][i]);

I need to get the username and its value from above json response.
I want to know what is correct way to get data from such json response.

Please suggest

Answer Source

Please try this:

var data=response[0].credentials;
for (var i=0; i<data.length; i++){

Here is jsfiddle