Created
September 26, 2016 16:39
-
-
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
This file contains 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
'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