user2848031 user2848031 - 1 year ago 217
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download