最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing JSON response body in Express.js - Stack Overflow

programmeradmin0浏览0评论

A Node.js/Express.js app makes a RESTful call to another app and receives JSON in response. But the JSON response is not being parsed into new variables. What specific changes need to be made to the code below, so that the JSON body can be successfully parsed into new variables that the receiving Node.js/Express.js app can use for further processing?

Here is the Node.js/Express.js code which is currently receiving the JSON body response:

var url = require('url');
var request = require('request');

app.get('/user**', function(req, res) {
    console.log("You Hit The User Route TOP");
    request.get(authServer + '/uaa/user', function (error, response, body) {
        if(error){console.log('ERROR with user request.')}
        if (!error){// && response.statusCode == 200) {
            console.log(response.statusCode); console.log(body);

             response.on('data', function(chunk){
                 console.log('inside response.on(data...)');
                 body += chunk;
             });

             response.on('end', function(){
                 console.log('inside response.on(end...)');
                 body = JSON.parse(body);
                 var text = '';
                 for (var key in body){
                      text += 'Index is: ' + key + 
                              '\nDescription is:  ' + body[key] 
                 }

                 // The Description is:  "descriptive string"  
                 console.log("Got a response: ", text);
                 res.send(text);            
             });
            res.send(body);
        };
    }).auth(null, null, true, bearerToken);//this inserts bearer token in the GET request
    console.log("You Hit The User Route BOTTOM");
});

Here are the nodemon logs for the GET shown in the code. Note that the response.on() blocks are never called because their SYSO never prints:

You Hit The User Route TOP
You Hit The User Route BOTTOM
200
{ long JSON string, which is formatted and truncated below for easier reading }
GET /user 200 182.862 ms - 1296

And here is the formatted and truncated JSON body, which illustrates the format of the data that needs to be parsed into Node.js/Express.js JavaScript variables:

{
    "details":  
        {
            "remoteAddress":"127.0.0.1",
            "sessionId":null,
            "tokenValue":"SomeLongTokenString",
            "tokenType":"Bearer",
            "decodedDetails":null
        },
    "authenticated":true,
    "userAuthentication":
        {
            "details":null,
            "authorities":
                [
                    {
                        "authority":"ROLE_ADMIN"
                    },
                    {
                        "authority":"ROLE_USER"
                    }
                ],
            "authenticated":true,
            "principal":"user",
            "credentials":"N/A",
            "name":"user"
        },
    "name":"user"
}

A Node.js/Express.js app makes a RESTful call to another app and receives JSON in response. But the JSON response is not being parsed into new variables. What specific changes need to be made to the code below, so that the JSON body can be successfully parsed into new variables that the receiving Node.js/Express.js app can use for further processing?

Here is the Node.js/Express.js code which is currently receiving the JSON body response:

var url = require('url');
var request = require('request');

app.get('/user**', function(req, res) {
    console.log("You Hit The User Route TOP");
    request.get(authServer + '/uaa/user', function (error, response, body) {
        if(error){console.log('ERROR with user request.')}
        if (!error){// && response.statusCode == 200) {
            console.log(response.statusCode); console.log(body);

             response.on('data', function(chunk){
                 console.log('inside response.on(data...)');
                 body += chunk;
             });

             response.on('end', function(){
                 console.log('inside response.on(end...)');
                 body = JSON.parse(body);
                 var text = '';
                 for (var key in body){
                      text += 'Index is: ' + key + 
                              '\nDescription is:  ' + body[key] 
                 }

                 // The Description is:  "descriptive string"  
                 console.log("Got a response: ", text);
                 res.send(text);            
             });
            res.send(body);
        };
    }).auth(null, null, true, bearerToken);//this inserts bearer token in the GET request
    console.log("You Hit The User Route BOTTOM");
});

Here are the nodemon logs for the GET shown in the code. Note that the response.on() blocks are never called because their SYSO never prints:

You Hit The User Route TOP
You Hit The User Route BOTTOM
200
{ long JSON string, which is formatted and truncated below for easier reading }
GET /user 200 182.862 ms - 1296

And here is the formatted and truncated JSON body, which illustrates the format of the data that needs to be parsed into Node.js/Express.js JavaScript variables:

{
    "details":  
        {
            "remoteAddress":"127.0.0.1",
            "sessionId":null,
            "tokenValue":"SomeLongTokenString",
            "tokenType":"Bearer",
            "decodedDetails":null
        },
    "authenticated":true,
    "userAuthentication":
        {
            "details":null,
            "authorities":
                [
                    {
                        "authority":"ROLE_ADMIN"
                    },
                    {
                        "authority":"ROLE_USER"
                    }
                ],
            "authenticated":true,
            "principal":"user",
            "credentials":"N/A",
            "name":"user"
        },
    "name":"user"
}
Share Improve this question asked Jul 7, 2016 at 20:49 DollarCoffeeDollarCoffee 6141 gold badge5 silver badges13 bronze badges 4
  • You're immediately printing out the body variable before you've done JSON.parse on it. Why would it be parsed at that point? – Mike Cluck Commented Jul 7, 2016 at 20:51
  • I meant on this line (console.log(response.statusCode); console.log(body);). You need to wait until it's been processed in the end event as peteb said. Either that or remove the end event entirely. Upon closer inspection, I don't even know why you have that there. You're acting as though body is being generated by a stream but it shouldn't be. – Mike Cluck Commented Jul 7, 2016 at 21:03
  • Like I said, get rid of those. They're pointless. Remove the response.on blocks and try just doing body = JSON.parse(body); console.log(typeof body); – Mike Cluck Commented Jul 7, 2016 at 21:08
  • 1 If it's saying object that means it has been parsed. JSON is a string which is parsed into an object. Then you can access your values by doing body.name, body.authorities, etc. Example – Mike Cluck Commented Jul 7, 2016 at 21:16
Add a ment  | 

2 Answers 2

Reset to default 5

The problem is you're acting as though response is a stream that's incrementally giving you the JSON but you've already proven to yourself that's not true with your first console.log(body) statement. Instead, you can parse body immediately and begin working on it. You can also simplify your request handler.

if (error) {
  console.log('ERROR with user request.')
  return res.sendStatus(500);
}

body = JSON.parse(body);
var text = '';
for (var key in body) {
  text += 'Index is: ' + key + '\nDescription is:  ' + body[key]
}
// The Description is:  "descriptive string"  
console.log("Got a response: ", text);
res.send(text);

The following line doesn't wait for your response to be parsed.

res.send(body);

Remove it and wait to respond from your response.on('end') event.


Edit to include restructured request

I'd structure your request differently. You're not streaming your response so there shouldn't be much reason to listen for response events. Also you can get rid of your need for JSON.parse() by letting request handle that for you by indicating that the returning body is JSON.

request({
    method: 'GET',
    url: authServer + '/uaa/user', 
    json: true, // indicates the returning data is JSON, no need for JSON.parse()
    auth: {
        user: null,
        password: null,
        sendImmediately: true,
        bearer: bearerToken 
    }
}, function (error, response, body) {
    if(error){
      console.log('ERROR with user request.');
      return res.sendStatus(500);  // Return back that an error occurred
    }
    else {
        console.log(response.statusCode); 
        console.log(body);

        var text = '';
        for (var key in body) {
            text += 'Index is: ' + key + '\nDescription is:  ' + body[key];
        }

        return res.status(200).send(text);       
     }  
});
发布评论

评论列表(0)

  1. 暂无评论