1

I am trying to decode a dict of base64 strings like this:

responses64 = {
'54sdf61': 'eyJyZXNwb2...', 
'23423vse': 'sdfwerfwewe...'
}

The data comes like this from an API query, so I can't change that part.

Normally, I'd do this to decode base64:

response = json.loads(base64.b64decode(response64))

But since I have adict now with many of these strings, I have go another way. The best I could come up with was a for-loop over the key values, which are stored in another list called IDs, and write the results into a dict:

responses = {}

for i in IDs:
    responses[i] = json.loads(base64.b64decode(responses64[i]))

However, this doesn't work, since b64decode doesn't like to operate on dicts:

TypeError: argument should be a bytes-like object or ASCII string, not 'dict'

Any idea would be appreciated...

DataWiz
  • 401
  • 6
  • 14
  • Umm... does `responses64.update((k, base64.b64decode(v)) for k, v in responses64.items())` do what you're after? – Jon Clements Apr 15 '19 at 13:55
  • Funny enough, this works as it gives me strings of dicts. However, when I try to convert those dicts with `json.loads()`, I get `'utf-8' codec can't decode byte 0xb2 in position 0: invalid start byte` – DataWiz Apr 15 '19 at 14:34
  • Pass a suitable `encoding=` to `json.loads()` ? – Jon Clements Apr 15 '19 at 14:35
  • Unfortunately that doesn't work. It seems like the API gives out some non-base64 encoded message when the string is empty, so I have to figure out a way to filter out these query results before decoding. – DataWiz Apr 15 '19 at 16:01
  • OK, so I ended up using `if not ... == '{'`: to filter out responses that returned an error message. Not very elegant, but it does the trick, since all valid responses will be base64 encoded and thus not start with a `{`. – DataWiz Apr 15 '19 at 16:11

1 Answers1

1

you need to use json Encoding

so either using with base64

responses[i] = json.loads(base64.b64decode(json.dumps(responses64[i])))

or

response = json.loads(json.dumps(responses64))
mooga
  • 3,136
  • 4
  • 23
  • 38
  • 1
    Hmm... this doesn't decode anything unfortunately. It just seems to put the base64 code into a string. Do you mean I should use this within the decode function? Because that results in the same error. – DataWiz Apr 15 '19 at 13:59
  • Yup , try that `response = json.loads(json.dumps(responses64))` – mooga Apr 15 '19 at 14:02
  • This also works up until the part where I want to loop over `json.loads()` again. The decoding seems to work, but json.loads doesn't seem to like my loop: `UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 0: invalid start byte` – DataWiz Apr 15 '19 at 14:39
  • json.loads dosen't need a loop – mooga Apr 15 '19 at 14:41
  • I have a dictionary of strings, resulting from your loop. Each string is another dictionary. It doesn't get flattened automatically, so I have to loop over these strings again in order to get my desired results, no? – DataWiz Apr 15 '19 at 14:51
  • check that https://stackoverflow.com/questions/24508726/how-to-encode-python-dictionary – mooga Apr 15 '19 at 14:58