2

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);

}
2
  • 4
    Can you just use console.log(JSON.stringify(data))? Commented Nov 15, 2016 at 20:19
  • You can do that with map: const arrAsStr = arr.map( (r) => JSON.stringify(r) ); Commented Nov 15, 2016 at 21:50

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great. Its working ..How to get specific field value from json ?
JSON.stringify() will take any javascript object and convert it into a json string. so it access a property inside JSON string you have to parse it back to js object. you can do this using JSON.parse("json string goes here")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.