Parsing JSON data from a URL - javascript

I'm trying to create function that can parse JSON from a url. Here's what I have so far:
function get_json(url) {
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
return response;
});
});
}
var mydata = get_json(...)
When I call this function I get errors. How can I return parsed JSON from this function?

In case someone is looking for an solution that does not involve callbaks
function getJSON(url) {
var resp ;
var xmlHttp ;
resp = '' ;
xmlHttp = new XMLHttpRequest();
if(xmlHttp != null)
{
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
resp = xmlHttp.responseText;
}
return resp ;
}
Then you can use it like this
var gjson ;
gjson = getJSON('./first.geojson') ;

Your return response; won't be of any use. You can pass a function as an argument to get_json, and have it receive the result. Then in place of return response;, invoke the function. So if the parameter is named callback, you'd do callback(response);.
// ----receive function----v
function get_json(url, callback) {
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
// call function ----v
callback(response);
});
});
}
// -----------the url---v ------------the callback---v
var mydata = get_json("http://webapp.armadealo.com/home.json", function (resp) {
console.log(resp);
});
Passing functions around as callbacks is essential to understand when using NodeJS.

The HTTP call is asynchronous, so you must use a callback in order to fetch a resultant value. A return call in an asynchronous function will just stop execution.
function get_json(url, fn) {
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
fn(response);
});
});
};
get_json(url, function(json) {
// the JSON data is here
});
In this example, function(json) {} is passed into the get_json() function as fn(), and when the data is ready, fn() is called, allowing you to fetch the JSON.

Related

CallbackHandler in Node.js

I have the following method in a .js file for an asynchronous network connection
function requestWatsonDiscovery(queryString) {
console.log('Query =', queryString);
if (typeof queryString !== 'undefined') {
var discoveryUrl = `something`
console.log('Total url =', discoveryUrl);
var options = {
host: 'gateway.watsonplatform.net',
path: discoveryUrl,
auth: auth
};
http.get(options, function(http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function(chunk) {
// append this chunk to our growing `data` var
//console.log(data);
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function() {
// you can use res.send instead of console.log to output via express
//console.log(data);
data = JSON.parse(data);
watsonReturnedText = parseData(data);
//console.log(watsonReturnedText);
//returnResult();
});
});
}
}
at the same time I am updating my UI in another .js file.I want to get notified when the above asynchronous method has completed. I understand I can use callbacks /promises to do it.Can you show me the syntax to write a promise or a call back.
Simply put, this should give you basic understanding of callback in your demo
function requestWatsonDiscovery (queryString, callback){ // <- new param
http.get(options, function (http_res) {
var data = "";
http_res.on("data", function (chunk) {
data += chunk;
});
http_res.on("end", function () {
var parseData =JSON.parse(data);
callback(parseData); // <- usage of new param
});
});
}
requestWatsonDiscovery(null, function(result) {
// result is your data;
console.log(result);
});
This is basically..
function requestWatsonDiscovery (queryString){
return new RSVP.promise((resolve, reject) => {
if (typeof queryString !== 'undefined') {
var discoveryUrl = `something`
var options = {
host: 'gateway.watsonplatform.net',
path: discoveryUrl,
auth : auth
};
http.get(options, function (http_res) {
var data = "";
http_res.on("data", function (chunk) {
data += chunk;
});
http_res.on("end", function () {
data =JSON.parse(data);
watsonReturnedText = parseData(data);
resolve(watsonReturnedText);
});
});
}
else { reject(); }
}); //end promise
}
When you call your requestWatsonDiscovery do this...
var watsonPromise = requestWatsonDiscovery(queryString);
watsonPromise.then((data) => {
//use your watson data
}).catch(() => {
//manage the error
});

In node, how can I ask a function to return only after it has gotten all of the data it needs?

I have a method called createMessage that I pass a request and a response.
When this method gets called, it currently returns undefined because it is ending before it retrieves all the data. Is there some way I can get it to wait?
createMessage: function(request, response) {
var totalData = '';
request.on('data', function(data) {
totalData += data.toString();
});
request.on('end', function() {
totalData = JSON.parse(totalData);
totalData.created_at = new Date();
return totalData;
})
});
}
Invoke a callback inside the end function as its asynchronous. This is the nature of node
createMessage: function(request, response, callback) {
var totalData = '';
request.on('data', function(data) {
totalData += data.toString();
});
request.on('end', function() {
totalData = JSON.parse(totalData);
totalData.created_at = new Date();
callback(totalData)
})
});
}
Taking #meder's answer one step further: you need to do this with a callback, you can't wait ... because JavaScript is single-threaded.
createMessage: function(request, response, callback) {
var totalData = '';
request.on('data', function(data) {
totalData += data.toString();
});
request.on('end', function() {
totalData = JSON.parse(totalData);
totalData.created_at = new Date();
callback(null, totalData); // all done
});
}
By convention in Node, the first parameter to the callback is an error, if any. Because it didn't error, the first parameter in this callback is null.

Node.js access the value of variable from another function

How do i get the value of myObject.myname, myObject.myage from the function getval? It returns undefined when i console.log it. Btw I'm using node js. Thanks.
var post = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
console.log('Response: ' + data);
var myObject = JSON.parse(data);
console.log('----------------------------------------------');
console.log(myObject.myname);
console.log(myObject.myage);
console.log('----------------------------------------------');
});
});
function getVal() {
//some code here
console.log(myObject.myname);
console.log(myObject.myage);
}
Declare myObject outside the anonymous function you pass to request (var myObject) instead of inside it. At present it is a local variable.
Call getVal() after the HTTP response has been received (otherwise it won't have been set yet). At present you aren't calling it at all.
There is a scope issue, can you try this instead?
var myObject = {};
var post = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
console.log('Response: ' + data);
myObject = JSON.parse(data);
console.log('----------------------------------------------');
console.log(myObject.myname);
console.log(myObject.myage);
console.log('----------------------------------------------');
});
});
function getVal() {
//some code here
console.log(myObject.myname);
console.log(myObject.myage);
}

NodeJS return http.request

Hi now I know that NodeJS is asynchronous (which I am still trying to get my head around to be honest).
The problem that I am currently facing is I have attempting to do a http.request to receive some JSON data. This is fine but what I need is to return this data to a variable. I believe I need to do a callback function? (from what I have read up on the matter)
The Code that I currently have:
var http = require('http');
pCLatLng = '';
function postCodeCheck() {
var pCode = {
host: 'geo.jamiethompson.co.uk',
path: "/" + 'SW1A2AA' + ".json"
};
http.request(pCode).on('response', function(response) {
var data = '';
response.on("data", function (chunk) {
data += chunk;
});
response.on('end', function () {
pCJSON = JSON.parse(data);
pCLatLng = pCJSON;
});
}).end();
}
console.log(pCLatLng);
This is obviously outputting "undefined"; I have tried returning the response.on('end') when having return "hi" or anything inside it instead NodeJS outputs the information about the site. If anyone can help with this it would be much appreciated.
console.log(pCLatLng); needs to be inside (or inside something called by) the response.on('end' callback. The value isn't available until that callback is fired.
Try something like:
function postCodeCheck(callback) {
var pCode = {
host: 'geo.jamiethompson.co.uk',
path: "/" + 'SW1A2AA' + ".json"
};
http.request(pCode).on('response', function(response) {
var data = '';
response.on("data", function (chunk) {
data += chunk;
});
response.on('end', function () {
callback(JSON.parse(data));
});
}).end();
}
postCodeCheck(function (pCLatLng)
{
console.log(pCLatLng);
});
You want something like this:
var http = require('http');
function postCodeCheck(cb) {
var pCode = {
host: 'geo.jamiethompson.co.uk',
path: "/" + 'SW1A2AA' + ".json"
};
http.request(pCode).on('response', function(response) {
var data = '';
response.on("data", function (chunk) {
data += chunk;
});
response.on('end', function () {
var pCJSON = JSON.parse(data);
cb(pCJSON);
});
}).end();
}
postCodeCheck(function(pCLatLng) { console.log(pCLatLng); });
Look carefully for the differences before using.
You'll need your postCodeCheck() function to take a callback as well, just like http.request. In the world of async, calling callbacks with results takes a similar role to returning results.

How to return values from functions making HTTP request in node.js?

The getStockValue() function is called from another javascript file in the following way:
var r=require("./stockfile");
var returedData = r.getStockValue());
here returnedData contains only "-START-".
My objective is to get the body object returned from the function, after receiving the response. I've tried putting a return statement into the 'close' event handler, but it didn't work.
How should I do that?
function getStockValue() {
var http = require('http');
var options = {
host: 'in.reuters.com',
path: '/finance/stocks/overview?symbol=RIBA.BO',
method: 'GET'
};
var body = "--START--";
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.on('data', function (chunk) {
body += chunk;
});
res.on('close', function () {
console.log("\n\nClose received!");
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
return body + '... recieved';
}
exports.getStockValue = getStockValue;
As this is an asynchronous operation if will return straight away and continue to run in the background, hense why you only receive -START-. You can resolve this with the help of a callback function. Heres how:
Call the function as follows:
r.getStockValue(function(result) {
var returedData = result
//... rest of your processing here
}));
and within the getStockValue function change to this:
function getStockValue(callback) {
...
res.on('data', function (chunk) {
body += chunk;
callback(body);
});
...
}

Categories

Resources