0

I have been struggling with decoding encoded string.

str2=b'W3t2YWx1ZToxMCxtb2RlOiJsb3cifSx7dmFsdWU6NSxtb2RlOiJpbnRlcm1lZGlhdGUifSx7dmFsdWU6MTAsbW9kZToiZXhwZXJ0In0se3ZhbHVlOjIsbW9kZToiYWRtaW4ifV0='

When I am trying decode above string. I am getting below result

print(str1.decode('utf-8'))

W3t2YWx1ZToxMCxtb2RlOiJsb3cifSx7dmFsdWU6NSxtb2RlOiJpbnRlcm1lZGlhdGUifSx7dmFsdWU6MTAsbW9kZToiZXhwZXJ0In0se3ZhbHVlOjIsbW9kZToiYWRtaW4ifV0=

Please help.

Not sure why decoding part is getting printed. I have been through docs, duplicate questions but couldn't find correct answer.

Expected result: [{value:10,mode:"low"},{value:5,mode:"intermediate"},{value:10,mode:"expert"},{value:2,mode:"admin"}]

Actual: W3t2YWx1ZToxMCxtb2RlOiJsb3cifSx7dmFsdWU6NSxtb2RlOiJpbnRlcm1lZGlhdGUifSx7dmFsdWU6MTAsbW9kZToiZXhwZXJ0In0se3ZhbHVlOjIsbW9kZToiYWRtaW4ifV0=

asked Apr 26, 2019 at 6:18

2 Answers 2

2

Your string is base64 encoded. Try:

import base64
str2=b'W3t2YWx1ZToxMCxtb2RlOiJsb3cifSx7dmFsdWU6NSxtb2RlOiJpbnRlcm1lZGlhdGUifSx7dmFsdWU6MTAsbW9kZToiZXhwZXJ0In0se3ZhbHVlOjIsbW9kZToiYWRtaW4ifV0='
base64.b64decode(str2).decode('utf8')

result:

'[{value:10,mode:"low"},{value:5,mode:"intermediate"},{value:10,mode:"expert"},{value:2,mode:"admin"}]'
answered Apr 26, 2019 at 6:21
Sign up to request clarification or add additional context in comments.

3 Comments

This worked. Thanks. Also is it possible to convert this string to list of maps?
@sudhir You could do it with json.loads, but you'll need to use a regex or something similar to put quotes around the keys like "value" to make it valid JSON. Maybe something like json.loads(re.sub(r'(\w+?):', r'"1円":', s))
json.loads is replacing double quotes with single quotes.I tried json.dumps but it is returning string instead of list of dictionaries
0

In fact that is a base64 encoding and not utf-8

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport

Well to decode it you will need the following

import base64
str2 = b"W3t2YWx1ZToxMCxtb2RlOiJsb3cifSx7dmFsdWU6NSxtb2RlOiJpbnRlcm1lZGlhdGUifSx7dmFsdWU6MTAsbW9kZToiZXhwZXJ0In0se3ZhbHVlOjIsbW9kZToiYWRtaW4ifV0="
plaintext = base64.b64decode(str2)
print(plaintext)
# out : b'[{value:10,mode:"low"},{value:5,mode:"intermediate"},{value:10,mode:"expert"},{value:2,mode:"admin"}]'
answered Apr 26, 2019 at 6:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.