DollarCoffee DollarCoffee - 5 months ago 83
JSON Question

Parsing JSON response body in Express.js

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

Answer

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);