0

I am storing json object into cookies in browser window, but when I read cookie for the same json object I stored it returns me follwing json object:

"j%3A%7B%22request%22%3A%22login%22%2C%22provider%22%3A%22facebook%22%2C%22idd%22%3A1%2C%22auth_token%22%3A%22EAACQG9aNqPcBAFaWZCGG1UGsb1wqxqzaopZAFr1B3ZAR5tnNGQnLGcmuaHFjmvusPcBI9k3k5m6U89K3vZB4ruxadPHZA945lQtpc8fINoKQDQqZBXYhX0j0ZCupdxWc2VP5b86MvWtTGzBvWSxgEqHM74ZBcEhWZBF5PKc41VwOWAgZDZD%22%2C%22auth_name%22%3A%22Tushar%20Bochare%22%2C%22auth_email%22%3A%22mytusshar%40gmail.com%22%2C%22auth_id%22%3A%221414815471964057%22%2C%22cognito_id%22%3A%22us-east-1%3A2eb23ae9-bb78-40f7-b614-efe145dc1b7b%22%2C%22accessKey%22%3A%22ASIAJSIGNL2VUL3WVVZQ%22%2C%22secretKey%22%3A%22kKrFQzIl%2F4%2BqNTRa2XN4HMHjvzdaZo988QKf%2FLjO%22%2C%22status%22%3A1%2C%22name%22%3A%22fb%20tush%22%2C%22city%22%3A%22Akola%22%2C%22email%22%3A%22mytusshar%40gmail.com%22%2C%22message%22%3A%22LOGIN%20SUCCESS%22%7D"

my original JSON object that I stored is:

{"request":"login","provider":"facebook","idd":1,"auth_token":"yuiyuiiiyiyiiyissflkjljwqxqzaopZAFr1B3ZAR5tnNGQnLGcmuaHFjmvusPcBI9k3k5m6U89K3vZB4ruxadPHZA945lQtpc8fINoKQDQqZBXYhX0j0ZCupdxWc2VP5b86MvWtTGzBvWSxgEqHM74ZBcEhWZBF5PKc41VwOWAgZDZD","auth_name":"xyz smith","auth_email":"[email protected]","auth_id":"3123213171964057","cognito_id":"us-east-2:21wd2fh5-bb78-40a6-b614-efe145df2c4c","accessKey":"ASIAQFXSYL2VUL3WVVZQ","secretKey":"kKrKJzIl/4+qNTRa2YS4JBYjvzdaZo100QKf/LjO","status":1,"name":"fb xyz","city":"starcity","email":"[email protected]"}

It is replcaing all special characters like single quotes, braces, colon, quomma with its ascii code. Is there any function in JS to directly retrieve json object from cookies?

3 Answers 3

2

You can use decodeURI(str)to decode your string. This function replace every %x characters.

Find doc here

Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSON.stringify() when storing "json object" & you can use JSON.parse() to read json object from cookie.

1 Comment

I have tried doing that but it's not working, it is showing error: Uncaught SyntaxError: Unexpected token % in JSON at position 0 at JSON.parse (<anonymous>)
0

You can use decodeURIComponent(str) to decode your string, which is especially useful if the string contains URLs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.