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.
response.on('data')function,datais already valid JSON. When you calldata.toString()you are destroying the object and turning it to a string, so you can't pull out what you want. Just use data as is. -- Edit -- Actually, never mind.. the data returned from that request is formatted in a weird, unhelpful way.. making it hard to get what you want. - Brian Glaz