Take this base64-encoded JSON string generated from JavaScript using JSON.stringify
and btoa
:
JavaScript
x
3
1
btoa(JSON.stringify({"é": "è"}))
2
"eyLpIjoi6CJ9"
3
I’m trying to decode it from Python. I’m doing:
JavaScript
1
4
1
>>> import base64
2
>>> import json
3
>>> json.loads(base64.b64decode("eyLpIjoi6CJ9"))
4
I’m getting a UnicodeDecodeError
:
JavaScript
1
2
1
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2: invalid continuation byte
2
What is the right way to properly decode this in Python?
Note: I’m using base64 encoding so this can be safely passed as an URL query string parameter.
Thanks!
Advertisement
Answer
You can also do in JavaScript
JavaScript
1
3
1
btoa(unescape(encodeURIComponent(JSON.stringify({"é": "è"}))))
2
base64 output: eyLDqSI6IsOoIn0=
3
though I think btoa works on just the extended ascii table, not all unicode characters.
Then in Python
JavaScript
1
3
1
json.loads(base64.b64decode("eyLDqSI6IsOoIn0=")
2
prints: {'é': 'è'}
3