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=
2 Answers 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"}]'
3 Comments
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))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"}]'