I have formatted a string to be used within a putItem call using DynamoDB SDK. This Long string (item I would like to add has lots of information) is stored in a variable and I should be able to use it within the Item {}
part of dynamoDdb.putItem()
.
However, when the variable is actually processed extra characters are added in causing the formatting to be incorrect for how an Item {}
should look.
See below:
itemToAdd = JSON.stringify(marshalled2, null,2)
newString = itemToAdd.slice(4, itemToAdd.length-1)
correct = newString.replaceAll(" ", '');
correct2 = correct.replaceAll("\n", "")
//console.log(newString)
var params = {
TableName: "Music",
Item: {
correct2
}
}
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
My issue is that the string correct2
has no slashes or \n
in it as I have removed them prior however when the string correct2 is used with the params object extra black slashes have been added into the string messing up the formatting.
An example of the string contained in correct2 would be as follows:
"AlbumData":{"S":"2B5C1828-6077-4ED1-89AD-13602A7AC08D"},"label":{"S":"EvolutionMediaMusic"},"Album":{"S":"BleakDrama"},"albumCode":{"S":"EMM101"},"releaseDate":{"S":"21/06/2016"},"description":{"S":"Contemporarynoircrimedrama:coldandforebodingcueswithtwistedsyntheticatmospheresandpulsingrhythms."},"credits":{"S":""},"tracks":{"L":[{"M":{"id":{"S":"984CA56C-3FA8-4F49-9C9E-3F96C237EE7E"},"trackNo":{"S":"1"},"albumCode":{"S":"EMM101"},"albumName":{"S":"BleakDrama"},"lengthOfTrack":{"S":"02:30"}, //more fields would go here
If I was to copy the text above and insert it into where the variable is there is no issue with the code. Using the javascript debug console the variable correct2 is set to the above however
params.Item = \"AlbumData\":{\"S\":\"2B5C1828-6077-4ED1-89AD-13602A7AC08D\"},\"label\":{\"S\":\"EvolutionMediaMusic\"},\"Album\":{\"S\":\"BleakDrama\"}, // and the patten repeats
What is causing the difference between correct2 and params.Item? I would expect them to be the same.
CodePudding user response:
Dude, its ok, just relax. Its called escaping