3

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!"}

1 Answer 1

4

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.

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

6 Comments

@CORY I missed out that line while copying from my code. I have done that and it hasn't worked
@CORY This is on server side (node.js) I am using chrome browser btw.
Yeah I just realized this was tagged with node.js. No script errors or anything? The JSON library is built in to node so I don't see why it wouldn't be working.
FYI response.body is the raw text response. response.getBody() should already return a parsed JSON response.
Yes you are right! I just checked that before you put your comment. Thanks for your help!
|

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.