Example

Example

Source:

Methods

(async, static) example()

An example of using this library to parse a .csv into a mapped .json
Source:
See:
Example

Example.js - using fs to write files and census-csv-parser members to process census data

const csv = require('census-csv-parser');
const Parser = csv.Parser;
const util = csv.util;

async function example(){
    const content = await fs.readFile(inputPath,'utf8'); // get the raw text
    let arr = util.csvArray(content,/","/); // turn text to csv
    // clean data
    arr = util.transpose(arr); // rotate array so rows become columns, columns become rows
    arr = util.chop(arr,/Margin of Error!!/,-1); // delete any row with the word Margin of Error!! in any cell
    arr = util.chopColumn(arr,0); // remove column 0
    arr = util.clear(arr,/Estimate!!/); //clear the word "Estimate!!" wherever it's found in any cell in the array
    arr = util.chop(arr, 0); // remove the first row
    arr = util.clear(arr); // clear quotation marks
    arr = util.numerify(arr); // turn string numbers to numbers
    // parse to js obj
    let parser = new Parser(arr); // set up a parser
    parser.setHeaders(0,0); // set headers rows and columns
    parser.setProps('row'); // this means the row headers will be attribute keys while top level object names will be column headers
    parser.propsToArray(/!!/); // split the properties into sub arrays for nested attributes
    let json = parser.mapProps(); // create the json
    json = JSON.stringify(json); // turn the json into a string
    await fs.writeFile(outputPath,json).catch((e)=> {console.log('error writing file'); console.log(e);}) // write to file

}
example().catch( (ex) => {
    console.log(`error!\n${ex}`) // catch errors
})