Skip to content

Instantly share code, notes, and snippets.

@eaton
Last active March 9, 2021 03:10
Show Gist options
  • Save eaton/4baae2733e00e3b4956bf0b8f27245ae to your computer and use it in GitHub Desktop.
Save eaton/4baae2733e00e3b4956bf0b8f27245ae to your computer and use it in GitHub Desktop.
11ty CSVtoJSON
// full API options at https://github.com/Keyang/node-csvtojson
const csv = require('csvtojson');
// Commonly-tweaked defaults
const options = {
delimiter: ",",
trim: true,
ignoreEmpty: false,
flatKeys: false,
checkColumn: false,
noheader: false,
headers: []
};
// Add CSV to list of data file types
module.exports.eleventyConfig.addDataExtension('csv', contents => csv(options)
.fromString(contents)
.then(result => { return result })
);
// Pull in file data
const jsonArray = await csv(options).fromFile(myCSVFilePath);
// Pull in remote data
const request=require('request')
const jsonArray = await csv().fromStream(request.get('http://mywebsite.com/mycsvfile.csv'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment