Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  87] [ 2]  / answers: 1 / hits: 15580  / 9 Years ago, thu, november 19, 2015, 12:00:00

We are creating a web service in Azure service using node.js to retrieve data from SQL db. We are using ClearDB to do the same.



While retriving the data its not comming in a proper JSON format. How can we convert the result sql object to JSON string.



Below is my code.



app.get('/android', function(request, response) {
pool.getConnection(function(err, connection) {
if(err) { handleErrorResponse(err, response); return; }
var sql = select projectname from taggedemployee where empname='[email protected]' and tagflag='accepted'
connection.query(sql, {}, function(err, results) {
connection.release(); // always put connection back in pool after last query
if(err) { handleErrorResponse(err, response); return; }
var proj = JSON.stringify(results);
console.log(proj);
console.log(proj[0].projectname);
for(var myKey in proj) {
console.log(key:+ myKey+, value:+proj[myKey]);
}
response.setHeader('Content-Type', 'application/json');
response.status(200).send(JSON.stringify(results) );

});
});
});


I cant manipulate the JSON string the returning string is



[{projectname: Dominos}]


I tried JSON.stringify but no luck. Please help me to fix this issue


More From » json

 Answers
15

You don't need JSON.stringify() actually. results is already your javascript object, which represents array of json objects. Just use



console.log(results[0].projectname);

[#64350] Tuesday, November 17, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;