kshnkvn
kshnkvn

Reputation: 966

How to decode string with unicode in python?

I have the following line:

%7B%22appVersion%22%3A1%2C%22modulePrefix%22%3A%22web-experience-app%22%2C%22environment%22%3A%22production%22%2C%22rootURL%22%3A%22/%22%2C%22

Expected Result:

{"appVersion":1,"modulePrefix":"web-experience-app","environment":"production","rootURL":"/","

You can check it out here. What I tried:

foo = '%7B%22appVersion%22%3A1%2C%22modulePrefix%22%3A%22web-experience-app%22%2C%22environment%22%3A%22production%22%2C%22rootURL%22%3A%22/%22%2C%22'

codecs.decode(foo, 'unicode-escape')

foo.encode('utf-8').decode('utf-8')

This does not work. What other options are there?

Upvotes: 0

Views: 224

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55963

The string is urlencoded. You can convert it by reversing the urlencoding.

from urllib import parse

s = '%7B%22appVersion%22%3A1%2C%22modulePrefix%22%3A%22web-experience-app%22%2C%22environment%22%3A%22production%22%2C%22rootURL%22%3A%22/%22%2C%22'

unquoted = parse.unquote(s)
unquoted
'{"appVersion":1,"modulePrefix":"web-experience-app","environment":"production","rootURL":"/","'

This looks like part of a larger JSON string. The complete object can be de-serialised with json.loads.

Upvotes: 2

Related Questions