Created
March 12, 2013 23:41
-
-
Save sansmischevia/5148109 to your computer and use it in GitHub Desktop.
convert DynamoDb JSON to regular JSON javascript objects
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
function Value(value) { | |
switch (typeof value) { | |
case "number": return {N: String(value)} | |
case "string": return {S: value} | |
} | |
if (value) switch (typeof value[0]) { | |
case "number": return {NS: value.map(String)} | |
case "string": return {SS: value} | |
} | |
throw new Error("Invalid key value type.") | |
} | |
Value.prototype = { | |
parse: function(data) { | |
var name = Object.keys(data)[0] | |
, value = data[name] | |
switch (name) { | |
case "S": | |
case "SS": | |
return value | |
case "N": | |
return Number(value) | |
case "NS": | |
return value.map(Number) | |
default: | |
throw new Error("Invalid data type: " + name) | |
} | |
} | |
} | |
module.exports = Value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a module which does this dynamodb-marshaler. Handles the "M", "L", "BOOL", and "NULL" types as well.