'I want to be able to access a particular key within a json object response returned in postgres using node js and express

The code :- app.post('/BSK', urlencodedParser, function(req, res){ client.query(Select * from public."mst_bskServices" where "Name" = '${req.body.Service}', (err, result)=>{ if(!err){ res.end(JSON.stringify(result.rows)); console.log(JSON.parse(JSON.stringify(result.rows))); } else{ console.log(err.message) }; }); client.end; });

This is the response :- [ { id: 1, Name: 'Aikyashree

                                                                                                                                           ',
statusapiurl: 'https://serv1.wbmdfcscholarship.org/Api/bsk_submit_report


                                                                                                                                                   ',   
statusapidoctype: 'DEMO                                              '

} ]



Solution 1:[1]

If you want to access id from your response you can perform:results[0].id; And name: results[0].name

And if you have multiple records to access records use for loop over the result key as follows:

for (var a = 0; a < Object.keys(result).length; a++) { result[a].id result[a].name }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jigyasa Chotwani