Reputation: 351
I have a string like this:
const str = { "_id": "x", "test": "rxh", "access_data": { "account" : "1", "password" : "r\u0026", "name" : "eu" } }
And i am using JSON.Parse to parse this string. The problem is that the parse understant the part "\u0026" of my password as a unicode, and transforms it to "&"
if I try JSON.Parse(str) the return is:
{
_id: 'x',
institution_id: 'rxh',
access_data: { account: '1', password: 'r&', name: 'eu' }
}
How can I avoid this transformation?
Upvotes: 0
Views: 40
Reputation: 120496
To encode a literal back slash, use two backslashes (\\
)
JSON.parse(String.raw`"r\\u0026"`) === String.raw`r\u0026`
If you're using JSON.stringify where you produce the JSON string, it should take care of this for you:
let dataBundle = { password: String.raw`r\u0026` };
console.log(JSON.stringify(dataBundle));
Also, please be very careful about passing passwords in plain text. I don't know your situation, but it's best to leave account sign-in to widgets that do that for you. They often salt passwords before sending them over the network.
Upvotes: 1