I am attempting to pull out individual rows and specific data from the census data using node.js JSON.parse.
const http = require('http');
function printStuff(statesInfo){
const statesPopulations = `${statesInfo} This is all states info`;
console.log(statesPopulations);
}
const request = http.get(`http://api.census.gov/data/2013/acs5?get=NAME,B01001_001E&for=state:*&key=4c2f7253819e5491c78ff2c5ed541fe95943854c`,
response => {
let body = "";
console.log('Status Code:', response.statusCode);
response.on('data', data=> {
body += data.toString();
});
response.on('end', () => {
const statePop = JSON.parse(body);
const statesInfo = JSON.parse(body);
printStuff(statesInfo);
})
});
Using console.log(body) the results show up as...
NAME,B01001_001E,state,Alabama,4799277,01,Alaska,720316,02,Arizona,6479703,04,Arkansas,2933369,05,California,37659181,06,Colorado,51 19329,08,Connecticut,3583561,09,Delaware.......
if I use console.dir(body), the results show up as....
[[NAME][B01001][state] ['Alabama', '4799927', '01'], [ 'Alaska', '720316', '02' ]] ....
all the way down to Puerto Rico. I am trying to pull specific things out but examples I've been using on Treehouse are set up all nice and neat where you can pull specifically labeled things out using nice things like profile.badges.length, but from what I can tell, none of these things are labelled. I would like to be able to say, pull Virginia's info out of there, or Delaware.
Most likely the examples you are seeing on Treehouse use an object with keys {Alabama: {...}}
instead of the array that you get back from census.gov [["Alabama", ...]]
.
To access California's population you need to get the 6th(index 5 since its 0 based) nested array from the parent. Which looks like this:
get the California array
console.log(statePop[5]); // outputs ["California","37659181","06"]
get the population of California by getting the second item in the California array.
console.log(statePop[5][1]); // outputs "37659181"
If you want a more human readable version(like Treehouse examples) you would have to create an object with keys from the array. You could do this easily with something like lodash or manually like so:
var popByState = {};
// loop through each state and assign values to object keyed by the state name
statePop.forEach(function(stateArr) {
popByState[stateArr[0]] = {population: stateArr[1]};
// Ex stateArr = ["California","37659181","06"]
// stateArr[0] is the name of the state
// stateArr[1] is the population
});
console.log(popByState.California.population) // outputs "37659181";
But then you have to be aware of states that have a space in their name like "New York". You can't use dot notation to access these(console.log(popByState.New York.population)
) you have to use brackets console.log(popByState['New York'].population)