I am confused about this issue i am having
I recieve this Json object as a Websocket message from a hardware device, the property uiAppMsg is encoded in base64 so I decode it and then want to reassign it to the uiAppMsg in the jsonObj but when I run this in my app, it does not update the value of jsonObj.uiAppMsg and in the following lines i can still see its value as the encoded string, when i try to do this in JSFiddle for example it decodes the string properly and I can see the objects attribute value updated as the decoded Json Object. Am i missing something basic here why it wouldnt be working in the app. So basically I want to reassign the uiAppMsg Property on the object with the decoded string.
let socketMsg = "{"msgType":"uiAppMsg","uiAppMsg":"eyJ0cmFuc2FjdGlvbkRhdGEiOnsiQnJkU3RhdHVzRGlzcGxheSI6eyJEaXNwbGF5VGV4dCI6IkFMUkVBRFkgbmV3bGluZSBPTkJPQVJEIiwidGltZXIiOiIiLCJjb2xvciI6IiJ9fSwidHJhbnNhY3Rpb25UaW1lb3V0RGF0YSI6eyJCcmRTdGF0dXNEaXNwbGF5Ijp7IkRpc3BsYXlUZXh0IjoiIiwidGltZXIiOiIiLCJjb2xvciI6IiJ9fX0="}"
var jsonObj = JSON.parse(socketMsg);
const decodedUIAppMsg = atob(jsonObj.uiAppMsg);
jsonObj.uiAppMsg = JSON.parse(decodedUIAppMsg);
console.log(jsonObj)
2
Answers
@cmgchess thanks for your suggestion, it works to reassign it that way!
Both
JSON.stringify
andatob
should work in Angular.Here are two alternatives that use destructuring/spread, and a functional approach that reduces the object.