How to handle json response using expressjs

Viewed 40

i am new in nodejs (using expressjs) and i am calling a rest api,i am getting response as json in console(with two keys "id" and "text") but now i want to get "text" from json response and want to pass in "assistant.ask",How can i do this ?

Here is my code

function (error, response, body) {
        if (!error && response.statusCode == 200) {
         console.log(body);  //working
         assistant.ask(body.text);  // not working            
        }
    else{
        console.log(error); 
        }
1 Answers

Since your response is array, you need to access first item:

function (error, response, body) {
    if (!error && response.statusCode == 200) {
         assistant.ask(body[0].text);     
    }
    else {
        console.log(error); 
    }
}
Related