FarmHand FarmHand - 5 months ago 23
JSON Question

Casting undefined JSON into new JSON object

Undefined JSON is returned to a Node.js/Express.js app from a URL api endpoint

http : // someserver:someport / some_api_url?_var1=1
. This undefined JSON input will always have the same format, and needs to be cast into a new JSON object by the receiving code (shown below). The new JSON object can then be processed in various ways, including being passed into HTML.


THE OP QUESTION:

What specific changes need to be made to the code below so that:

1.) JSON in the form of
{"1":"descriptive string"}
gets cast into a newly defined JSON object called
dataElement
with two properties,
dataElement.dataKey
and
dataElement.descriptionString
?

2.) The html response sent back to the user's web browser includes
dataElement.dataKey
and
dataElement.descriptionString
in the UI format:

Index is: 1
Description is: descriptive string



EXAMPLE CODE TO BE MODIFIED:

For starters, the code that needs to be modified is:

app.get('/', function (req, res) {

var url = 'http://someserver:someport/some_api_url?_var1=1'

http.get(url, function (resInner) {
var body = '';

resInner.on('data', function (chunk) {
body += chunk;
});

resInner.on('end', function () {
var fullResponse = JSON.parse(body);

// code to parse JSON into new JSON object, which is passed into HTML
var indexStr = key;
var descriptionStr = fullResponse[key];
var dataElement = {"dataKey" : key, "descriptionString" : descriptionStr};
var htmlResp = 'Index is: '+${dataElement.dataKey}+'<br> Description is: '+${dataElement.descriptionString};
res.send(htmlResp);

});
}).on('error', function (e) {
console.log("Got an error: ", e);
});

});



THE CURRENT ERROR:

At the moment, the above code is giving the following error:

/home/user/nodejs_apps/express_helloworld/myapp/app.js:139
var htmlResp = 'Index is: '+${dataElement.dataKey}+'<br> Description is: '+${dataElement.descriptionString};
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:142:18)
at node.js:939:3

Answer

The error is coming from your string concatenation, which seems to be using some invalid templating language, and the key variable, which isn't defined, you have to actually get the keys with Object.keys etc.

Try it this way instead

app.get('/', function(req, res) {

    var url = 'http://someserver:someport/some_api_url?_var1=1'

    http.get(url, function(resInner) {
        var body = '';

        resInner.on('data', function(chunk) {
            body += chunk;
        });

        resInner.on('end', function() {
            var fullResponse = JSON.parse(body);

            // code to parse JSON into new JSON object, which is passed into HTML
            var keys     = Object.keys(fullResponse);
            var firstKey = keys[0];
            var descriptionStr = fullResponse[firstKey];
            var dataElement = {
                "dataKey": firstKey,
                "descriptionString": descriptionStr
            };
            var htmlResp = 'Index is: ' + dataElement.dataKey + '<br> Description is: ' + dataElement.descriptionString;

            res.send(htmlResp);
        });
    }).on('error', function(e) {
        console.log("Got an error: ", e);
    });

});