Skip to content

Instantly share code, notes, and snippets.

@igorzg
Last active October 27, 2022 15:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igorzg/c80c0de4ad5c4028cb26cfec415cc600 to your computer and use it in GitHub Desktop.
Save igorzg/c80c0de4ad5c4028cb26cfec415cc600 to your computer and use it in GitHub Desktop.
Dynamo db json re mapper
/**
MIT License
Copyright (c) 2016 Igor Ivanovic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
/**
* 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;
}
}
@dwaldmannDE
Copy link

Hi @igorzg

can you check why this example is not working as expected?

` var source2 = '{"spike_id": {"S": "57"}, "Day": {"S": "Monday"} }'

    function mapper(data) {

    let S = "S";
            let N = "N";
            let B = "B";
            let BOOL = "BOOL";
            let NULL = "NULL";
            let M = "M";
            let L = "L";
            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(B)) {
    data[key] = types[B];
    } 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]);
    }
    }

    return data;
            function isObject(value) {
            return typeof value === "object" && value !== null;
            }
    }

var result = (mapper(source2));
console.log(result);`

Will print {"spike_id": {"S": "57"}, "Day": {"S": "Monday"} } which seems to be wrong.
Thank you!

@igorzg
Copy link
Author

igorzg commented May 9, 2017

Hi @dwaldmannDE

You have to convert your json string to object.

 var source2 = JSON.parse('{"spike_id": {"S": "57"}, "Day": {"S": "Monday"} }');
mapper(source2)

Will give you expected result!

@kestess
Copy link

kestess commented May 16, 2017

What is the license for this? I'd love to grab and go since dynamodb streams sends data in the inconvenient format, but I want to make sure you're ok with it first.

@igorzg
Copy link
Author

igorzg commented Jul 9, 2017

@kestess its MIT

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