Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.parse unexpected result in node.js

I am sending a http request to a website (from node.js) which returns a JSON object. I get the expected JSON file. However when I parse the JSON text, my program isn't able to do anything.

var URL = 'http://www.omdbapi.com/?t=' + movie + '&y=&plot=short&r=json';
requestify.get(URL).then(function (response) {
    console.log(response.getBody()); // It prints correctly
    var jsonBody =  response.getBody();
    var jsonObject = JSON.parse(jsonBody);
    if (jsonObject.Response == 'False') {
        console.log('False'); //not printed
    } else {
        console.log('true'); //Not printed
    }
});

Sample JSON output:

{"Response":"False","Error":"Movie not found!"}
like image 668
user1692342 Avatar asked Feb 15 '26 18:02

user1692342


1 Answers

response.body is the raw text response. response.getBody() should already return a parsed JSON response as long as you have the correct content-type header specified.

Sending a JS object to JSON.parse results in a SyntaxError.

like image 139
Cᴏʀʏ Avatar answered Feb 17 '26 07:02

Cᴏʀʏ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!