Last active
December 10, 2019 08:45
-
-
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.
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
// 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