Skip to content

Instantly share code, notes, and snippets.

@chrisvogt
Last active December 10, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvogt/692d8849d9988daae8f6e0a6c05bed1b to your computer and use it in GitHub Desktop.
Save chrisvogt/692d8849d9988daae8f6e0a6c05bed1b to your computer and use it in GitHub Desktop.
Small utility script to convert lists of presidential candidates – found on the fec.gov website – from CSV to JSON.
// NOTE(cvogt): parses documents from the FEC registered candidates table
// located at https://www.fec.gov/data/candidates/?election_year=2020&office=P
const csvToJson = require('csvtojson/v2');
const candidatesFile = './candidates-running-2016-2019-12-09T23_23_05.csv';
const bracketsStringToArray = str => {
// HACK(cvogt): this can be optimized, and is fragile – throws upon {00,00}
const itemJsonString = str.replace('{', '[').replace('}', ']');
const items = JSON.parse(itemJsonString);
return items;
}
(async () => {
try {
const parsed = await csvToJson({
colParser: {
cycles: item => bracketsStringToArray(item),
election_years: item => bracketsStringToArray(item)
}
}).fromFile(candidatesFile);
console.log(require('util').inspect(parsed));
} catch (err) {
console.error(err)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment