Last active
December 21, 2015 13:39
-
-
Save techstep/6314639 to your computer and use it in GitHub Desktop.
Task: get GDP data from the new JSON-ified FRED API. Works on command line in node, but not so much in a browser (Chrome 29).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gdp_get() { | |
var fred_api_key = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
var fred_gdp_url = "http://api.stlouisfed.org/fred/series/observations?series_id=GNPCA&api_key="+fred_api_key+"&file_type=json&callback=?"; | |
var x = $.getJSON(fred_gdp_url, function(data, status){ | |
return data; | |
}); | |
var gdp = ''; | |
if (x.hasOwnProperty("responseText")) { | |
gdp = $.parseJSON(x.responseText); | |
} | |
var gdpObs = gdp.observations; | |
gdpObs.forEach( | |
function(element, index, array) { | |
element['x'] = Math.floor(new Date(element['date']).getTime()/1000); | |
element['y'] = Number(element['value']); | |
// time to strip out stuff | |
delete element['realtime_start']; | |
delete element['realtime_end']; | |
delete element['date']; | |
delete element['value']; | |
}); | |
return gdpObs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the following error:
"Uncaught SyntaxError: Unexpected token: http://api.stlouisfed.org/fred/series/observations?series_id=GNPCA&api_key=abcdefghijklmnopqrstuvwxyz&file_type=json&callback=jQuery110109660641909576952_1377221124798&_=1377221124799"
When I actually follow the URL (API key removed), I get the data I expect.
Tried looking at StackOverflow etc, but am finding that I know enough to make me dangerous, but not enough to make sense of the conversations.