Last active
October 22, 2017 21:19
-
-
Save puppybits/d1280eabd8aaa74ae37b5fa00afe671e to your computer and use it in GitHub Desktop.
Convert DynamoDB Export to standard JSON
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
let file = './data.dynamodata'; | |
function mapper(data) { | |
const val = obj => Object.keys(obj).reduce((o,k) => o[k],obj); | |
const defaultFn = (obj, key) => ({[key]: val(obj)}); | |
const recurFn = (obj, key) => ({[key]: mapper(val(obj))}); | |
const valType = { | |
"NULL": (obj, key) => ({[key]:null}), | |
"BOOL": defaultFn, | |
"SS": defaultFn, | |
"BB": defaultFn, | |
"NS": defaultFn, | |
"BS": defaultFn, | |
"S": defaultFn, | |
"L": recurFn, | |
"M": recurFn, | |
"N": (obj, key) => ({[key]: parseFloat(obj["N"])}), | |
} | |
let isArray = Array.isArray(data); | |
let result = (isArray ? data : [data]).map(dynamoRow => { | |
return (isArray ? | |
(() => { | |
let type = Object.keys(dynamoRow)[0]; | |
let convert = valType[type]; | |
return convert(dynamoRow, "key")["key"]; | |
})() : | |
Object.keys(dynamoRow) | |
.reduce((obj, key) => { | |
let type = Object.keys(dynamoRow[key])[0]; | |
let convert = valType[type]; | |
return Object.assign(obj, convert(dynamoRow[key], key)) | |
}, {})); | |
}); | |
return (isArray ? result : result[0]); | |
} | |
var ws = require('fs').createWriteStream(file+".json", {encoding: 'utf8'}); | |
ws.isTTY = true; | |
ws.write('[\n'); | |
var lineReader = require('readline').createInterface({ | |
input: require('fs').createReadStream(file) | |
}); | |
lineReader.on('line', (line) => { | |
ws.write(JSON.stringify(mapper(JSON.parse(line))) + ',\n'); | |
}) | |
lineReader.on('close', () => { ws.write(']'); }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment