0

When I print the following

console.log('\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you') console.log(utf8.decode('\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you')) 

the result rightly is

थडथडदय followed you - (i) थडथडदय followed you - (ii) 

When I access a redis list using redis.lrange('notif-'+userId, 0, -1) its first element is displayed as

["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "some_url", 201, "Users"] 

(note that above is a list stored as a string in redis using redis.lpush('notif-'+userId, python-list), its the first item of a redis list)

Since the above can't be put into JSON.parse because of \x, I escape the slash and then revert using

let notificationList = JSON.parse(notificationParent.replace(/\\/g, '\\\\')) notification.text = notificationList[0].replace(/\\\\/g, '\\') 

Now, when I console.log(notification.text) and console.log(utf8.decode(notification.text)), what gets printed is

\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you \xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you 

What should I do to get results similar to (i) and (ii)?

EDIT: From the beginning, if I do the following

 console.log(notificationParent) notificationParent = notificationParent.replace(/'/g, '"'); console.log(notificationParent) let notificationList = JSON.parse(notificationParent.toString()) console.log(notificationList) console.log(JSON.parse('["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "some_url", 201, "Users"]')) 

the result is

['\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you', 'Users', '233', 'https://storage.googleapis.com/humbee_images/cartoon-bee-1.jpg', 201, 'Users'] ["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "https://storage.googleapis.com/humbee_images/cartoon-bee-1.jpg", 201, "Users"] Syntax error: Unexpect token x in position 3 [ 'थडथडदय followed you', 'Users', '233', 'some_url', 201, 'Users' ] 

I don't understand the difference between the third and fourth print statement. Isnt the variable in 3rd containing the same string as 4th?

SOLVED: Joe's comment solved this puzzle. The second print although printing the variable with a single \ is actually double escaped, so the double escape needs to be converted by the replace function suggested in Joe's comment.

1 Answer 1

1

You actually can put it into JSON.parse with \x. Are you sure you are parsing the string (and not the array containing a string)?

JSON.parse('["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "some_url", 201, "Users"]') => ["थडथडदय followed you", "Users", "233", "some_url", 201, "Users"] 

vs.

JSON.parse(["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "some_url", 201, "Users"]) => Uncaught SyntaxError: Unexpected token , 
Sign up to request clarification or add additional context in comments.

8 Comments

In let notificationList = JSON.parse(notificationParent.replace(/\\/g, '\\\\')), had I been passing an array containing a string, would I have been able to call .replace(). Cant I thus be sure that notificationParent and the result after replace are indeed a string? I tried let notificationList = JSON.parse(notificationParent.toString()) and got Unexpected token x in JSON at position 3.
I got to know that \x is not allowed from the answer at stackoverflow.com/questions/27059765/…
Some other answer somewhere suggested to replace \x with \u00. That didnt help too and anyway that solution isnt feasible for me since the some_url part above also contains \x and I dont want to disturb that.
JSON.parse should not even see the \x because it will be replaced before going into the function: console.log('\xe0') => à vs. console.log('\xe' + '0') => SyntaxError: Invalid hexadecimal escape sequence
If your string contains double slashes one can unescape it like this: '\\xe0\\xa4\\xa5\\xe0\\xa4\\xa1\\xe0\\xa4\\xa5\\xe0\\xa4\\xa1\\xe0\\xa4\\xa6\\xe0\\xa4\\xaf followed you'.replace(/\\x([0-9a-f]{2})/g, function(_, pair) { return String.fromCharCode(parseInt(pair, 16)); }) => "थडथडदय followed you"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.