user2848031 user2848031 - 23 days ago 6
Node.js Question

How to convert from array of json object to String in Nodejs?

I'm getting response from HTTP post as array of json objects. but want to display as string. Please see my response..

{ total: 14,
results:
[ { name: [Object],
comments: [],
attributes: [Object],
type: [Object],
context: [Object],
status: 'Accepted',
score: 1.7646775,
modified: 1426085315767,
parents: [] },
{ name: [Object],
comments: [],
attributes: [Object],
type: [Object],
context: [Object],
status: 'Accepted',
score: 1.3945999,
modified: 1425386916807,
parents: [] },


Below code to display response.

function(error, response, data){
if(error) {
console.log(error);
} else {
console.log(data);

}

Answer

NodeJS has ES5 JSON class which has stringify() method in it

function(error, response, data){
    if(error) {
      console.log(error);
    } else {
      console.log(JSON.stringify(data));
}

JSON.stringify