Skip to content

Instantly share code, notes, and snippets.

@sansmischevia
Created March 12, 2013 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sansmischevia/5148109 to your computer and use it in GitHub Desktop.
Save sansmischevia/5148109 to your computer and use it in GitHub Desktop.
convert DynamoDb JSON to regular JSON javascript objects
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
@nackjicholson
Copy link

I wrote a module which does this dynamodb-marshaler. Handles the "M", "L", "BOOL", and "NULL" types as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment