0

I have a NodeJS REST API. In that API, I have to call another 3rd party API to get some data. It gives us data as JSON. This is how I set that response of data to my REST url response.

request('http://dev.demo.com/api/v1/wow/getFreeviewLoginAndCallAPI/123', function (error, response, body) {
if (!error && response.statusCode == 200) {
    console.log(body);
    res.json(body);
}

});

And then I get a response like this.

"{\"Response\":{\"@attributes\":{\"Package\":\"MONTHLY\",\"Surname\":\"Taylor\",\"CSN\":\"104801\",\"Email\":\"adam.taylor@gmail.com\",\"MiscInformation\":\"\",\"Message\":\"CUSTOMER_ACTIVE\"}},\"Submission\":{\"@attributes\":{\"SubmitterRef\":\"0778798654\",\"Submitter\":\"demoName\"}}}"

But when I use that 3rd party URL in my browser , I get a clean and nice JSON output. Like the following.

(without \ symbol)

No other "\" symbols and it's clean. What I need to do is I need to access some values of this response. In the node js. I need to send the response as follows.

res.json(body.Response);

But it's null. And also I need to take all these values to variables. How can I do that ?

Root SL
  • 69
  • 1
  • 6
  • That looks like a string. Have you tried parsing it? http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – José Luis Dec 26 '16 at 19:02

1 Answers1

1

The response from you third part api is as string , you probably need to parse before sending to client .

request('http://dev.demo.com/api/v1/wow/getFreeviewLoginAndCallAPI/123', function (error, response, body) {
if (!error && response.statusCode == 200) {        
    res.json( JSON.parse(body));
}});

You can not use body.Response directly because its string not object,firstly parse it and then send to client ,like

var json = JSON.parse(body); 
res.json(json.Response);
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80