-
-
Save kmikzjh/cd84480503bfde14cb9efee2fc7aabbe to your computer and use it in GitHub Desktop.
Dynamo db json re mapper
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
/** | |
* Dynamodb types | |
S – String | |
N – Number | |
B – Binary | |
BOOL – Boolean | |
NULL – Null | |
M – Map | |
L – List | |
SS – String Set | |
NN – Number Set | |
BB – Binary Set | |
This function remapps dynamodb dsl json to normal json object | |
FROM: | |
{ | |
"spike_id": { | |
"S": "57" | |
}, | |
"Day": { | |
"S": "Monday" | |
} | |
} | |
TO: | |
{ | |
"spike_id": "57", | |
"Day": "Monday" | |
} | |
*/ | |
function mapper(data) { | |
let S = "S"; | |
let SS = "SS"; | |
let NN = "NN"; | |
let NS = "NS"; | |
let BS = "BS"; | |
let BB = "BB"; | |
let N = "N"; | |
let BOOL = "BOOL"; | |
let NULL = "NULL"; | |
let M = "M"; | |
let L = "L"; | |
if (isObject(data)) { | |
let keys = Object.keys(data); | |
while (keys.length) { | |
let key = keys.shift(); | |
let types = data[key]; | |
if (isObject(types) && types.hasOwnProperty(S)) { | |
data[key] = types[S]; | |
} else if (isObject(types) && types.hasOwnProperty(N)) { | |
data[key] = parseFloat(types[N]); | |
} else if (isObject(types) && types.hasOwnProperty(BOOL)) { | |
data[key] = types[BOOL]; | |
} else if (isObject(types) && types.hasOwnProperty(NULL)) { | |
data[key] = null; | |
} else if (isObject(types) && types.hasOwnProperty(M)) { | |
data[key] = mapper(types[M]); | |
} else if (isObject(types) && types.hasOwnProperty(L)) { | |
data[key] = mapper(types[L]); | |
} else if (isObject(types) && types.hasOwnProperty(SS)) { | |
data[key] = types[SS]; | |
} else if (isObject(types) && types.hasOwnProperty(NN)) { | |
data[key] = types[NN]; | |
} else if (isObject(types) && types.hasOwnProperty(BB)) { | |
data[key] = types[BB]; | |
} else if (isObject(types) && types.hasOwnProperty(NS)) { | |
data[key] = types[NS]; | |
} else if (isObject(types) && types.hasOwnProperty(BS)) { | |
data[key] = types[BS]; | |
} | |
} | |
} | |
return data; | |
function isObject(value) { | |
return typeof value === "object" && value !== null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment