Python – Data decoded by base64 applied errors in json.loads().

Data decoded by base64 applied errors in json.loads()…. here is a solution to the problem.

Data decoded by base64 applied errors in json.loads().

I’m trying to use json.loads() in python

I get the error :

The JSON object must be str, not ‘bytes’

a = {'h': '123', 'w': '12345', 'data': "b'eyJod2lkIjpwomfcwpvepovnepovqrepniLLKJAMSNDMSNDMAWEFMOEDAad='"}

a.update(json.loads(base64.b64decode(a['data'])))

Here the “Data” part of A is loaded as a JSON dump encoded with B64.

'data':base64.b64encode(json.dumps(test).encode()); where test = some string eg('epovqrepniLLKJAMSNDMSNDMAWEFMOEDAad=')

I’ve tried using :

a.update(json.loads(base64.b64decode(a['data']).decode('utf-8')))

Give me one
The “utf-8” codec could not decode bytes in position: invalid contiguous bytes

I’ve also tried using decodebytes instead of b64decode to no avail.

I would appreciate any help!

Solution

Thank you all for your help.

After doing a lot of searching on Stackoverflow and testing on my local machine, I was able to dive into this.

The object passed in (a['data']) has some values that are not UTF-8 decodeable.

It is in the form of b'xxxsknoen'

I ended up removing b and the preceding and trailing quotes and then converted it to str.

var = base64.b64decode(str(a['data'])[2:-1]).decode('utf-8')
a.update(json.loads(var))

Related Problems and Solutions