0

Take this base64-encoded JSON string generated from JavaScript using JSON.stringify and btoa:

btoa(JSON.stringify({"é": "è"}))
"eyLpIjoi6CJ9"

I'm trying to decode it from Python. I'm doing:

>>> import base64
>>> import json
>>> json.loads(base64.b64decode("eyLpIjoi6CJ9"))

I'm getting a UnicodeDecodeError:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2: invalid continuation byte

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!

1 Answer 1

1

You can also do in JavaScript

    btoa(unescape(encodeURIComponent(JSON.stringify({"é": "è"}))))
    base64 output: eyLDqSI6IsOoIn0=

though I think btoa works on just the extended ascii table, not all unicode characters.

Then in Python

    json.loads(base64.b64decode("eyLDqSI6IsOoIn0=")
    prints: {'é': 'è'}
Sign up to request clarification or add additional context in comments.

1 Comment

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.