0

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?

1 Answer 1

2

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.

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

2 Comments

Thanks. Is there another way, without using a urllib?
Not that I know of. I wouldn't be too difficult to write your own decoder, using the information here, but given that urllib is in the standard library it seems rather pointless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.