Skip to content

Instantly share code, notes, and snippets.

@meshulam
Created September 26, 2016 16:39
Show Gist options
  • Save meshulam/a927b44ab292f772b54f822a097f7a5f to your computer and use it in GitHub Desktop.
Save meshulam/a927b44ab292f772b54f822a097f7a5f to your computer and use it in GitHub Desktop.
Transform an array-of-arrays style JSON table file (with header row) into an array-of-objects format
'use strict';
const fs = require('fs');
const files = process.argv.slice(2);
function transformRow(row, headers) {
let outObj = {};
for (let i=0; i<headers.length; i++) {
outObj[headers[i]] = row[i];
}
return outObj;
}
for (let filename of files) {
let tableJson = require("./" + filename)['data'];
let headers;
let outRows = [];
for (let row of tableJson) {
if (!headers) {
headers = row;
} else {
outRows.push(transformRow(row, headers));
}
}
fs.writeFileSync("output/" + filename, JSON.stringify(outRows, null, 2));
console.log("wrote " + filename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment