Last active
October 18, 2018 18:34
-
-
Save michaelwclark/970861d392f963e4ec32862ba4235461 to your computer and use it in GitHub Desktop.
Take in JSON files, flatten then and output as CSV.
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
import fs from 'fs' | |
function reduceJsonToCSV (jsonData, prefix = '') { | |
let parsedData = jsonData | |
try { | |
parsedData = JSON.parse(parsedData) | |
} catch (e) {} | |
const flatObj = Object.entries(parsedData).reduce((accum, [k, v]) => { | |
const cellPrefix = prefix ? `${prefix}.${k}` : k | |
if (Array.isArray(v)) { | |
const reducedDataArray = reduceDataArray(v, cellPrefix) | |
return { ...accum, ...reducedDataArray } | |
} | |
if (typeof v === 'object') { | |
return { ...accum, ...reduceJsonToCSV(v, cellPrefix) } | |
} | |
return { ...accum, [cellPrefix]: v } | |
}, {}) | |
return flatObj | |
} | |
function reduceDataArray (arr, prefix = '') { | |
return Object.values(arr).reduce((accum, curr) => { | |
return { ...accum, [`${prefix}.${curr.action_type}`]: curr.value } | |
}, {}) | |
} | |
function parseJsonToCSV (JSONData) { | |
return Object.entries(reduceJsonToCSV(JSONData)).join('\n') | |
} | |
function getJsonFiles (dir) { | |
const allFiles = fs.readdirSync(dir) | |
return allFiles.filter(x => x.indexOf('.json') > -1) | |
} | |
function parseFilesToCSV (dir) { | |
const JSONFiles = getJsonFiles(dir) | |
JSONFiles.map(file => { | |
const jsonData = fs.readFileSync(dir + file) | |
const csvData = parseJsonToCSV(jsonData) | |
fs.writeFileSync(dir + file.replace('.json', '.csv'), csvData) | |
}) | |
} | |
parseFilesToCSV('./data/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment