What is the recommended way to remove data type descriptors from a DynamoDB response?
They have a converter that you can use.
For instance, here is their example:
var data= {"Item": {
"Age": {"N": "8"},
"Name": {"S": "Fido"},
"Vaccinations": {
"M": {
"Rabies": {
"L": [
{"S": "2009-03-17"},
{"S": "2011-09-21"},
{"S": "2014-07-08"}
]
},
"Distemper": {"S": "2015-10-13"}
}
}}};
var marshalled = AWS.DynamoDB.Converter.unmarshall(data);
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html
Maybe like this:
var data= {"Item": {
"Age": {"N": "8"},
"Name": {"S": "Fido"},
"Vaccinations": {
"M": {
"Rabies": {
"L": [
{"S": "2009-03-17"},
{"S": "2011-09-21"},
{"S": "2014-07-08"}
]
},
"Distemper": {"S": "2015-10-13"}
}
}}};
var keys_black_list=['N', 'S', 'M', 'L'];
function correct_data(_data){
var new_data={};
for(var key in _data){
if (typeof _data[key] === 'object') {
for(var bl_key in keys_black_list){
if(_data[key][keys_black_list[bl_key]]){
if(typeof _data[key][keys_black_list[bl_key]] === 'object'){
new_data[key]=correct_data(_data[key][keys_black_list[bl_key]]);
}else{
new_data[key]=_data[key][keys_black_list[bl_key]];
}
}
}
}
}
return new_data;
}
console.log(correct_data(data.Item));
Not familiar with working with DynamoDb. My guess is there are SDK's you can use to do this for you, or settings in queries that can be used to turn off the descriptors
Following seems to work for all the cases in your sample
var descriptors = ['L', 'M', 'N', 'S'];
function flatten(o) {
// flattens single property objects that have descriptors
for (let d of descriptors) {
if (o.hasOwnProperty(d)) {
return o[d];
}
}
Object.keys(o).forEach((k) => {
for (let d of descriptors) {
if (o[k].hasOwnProperty(d)) {
o[k] = o[k][d];
}
}
if (Array.isArray(o[k])) {
o[k] = o[k].map(e => flatten(e))
} else if (typeof o[k] === 'object') {
o[k] = flatten(o[k])
}
});
return o;
}
data = flatten(data)
console.log(data)
.as-console-wrapper {
max-height: 100%;
}
<script>
var data = {"Item":{"Age":{"N":"8"},"Name":{"S":"Fido"},"Vaccinations":{"M":{"Rabies":{"L":[{"S":"2009-03-17"},{"S":"2011-09-21"},{"S":"2014-07-08"}]},"Distemper":{"S":"2015-10-13"}}}}};
</script>