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!"}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With