In Python 3.x you need to convert your str object to a bytes object for base64 to be able to encode them. You can do that using the str.encode method:
>>> import json
>>> import base64
>>> d = {"alg": "ES256"}
>>> s = json.dumps(d) # Turns your json dict into a str
>>> print(s)
{"alg": "ES256"}
>>> type(s)
<class 'str'>
>>> base64.b64encode(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/base64.py", line 56, in b64encode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
>>> base64.b64encode(s.encode('utf-8'))
b'eyJhbGciOiAiRVMyNTYifQ=='
If you pass the output of your_str_object.encode('utf-8') to the base64 module, you should be able to encode it fine.
In Python 3.x you need to convert your str object to a bytes object for base64 to be able to encode them. You can do that using the str.encode method:
>>> import json
>>> import base64
>>> d = {"alg": "ES256"}
>>> s = json.dumps(d) # Turns your json dict into a str
>>> print(s)
{"alg": "ES256"}
>>> type(s)
<class 'str'>
>>> base64.b64encode(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/base64.py", line 56, in b64encode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
>>> base64.b64encode(s.encode('utf-8'))
b'eyJhbGciOiAiRVMyNTYifQ=='
If you pass the output of your_str_object.encode('utf-8') to the base64 module, you should be able to encode it fine.
You could encode the string first, as UTF-8 for example, then base64 encode it:
data = '{"hello": "world"}'
enc = data.encode() # utf-8 by default
print base64.encodestring(enc)
This also works in 2.7 :)
Trying to print out json from a decoded base64 - Python - Stack Overflow
Decode a base64 encoded JSON string generated from JavaScript in Python - Stack Overflow
python - extract Json file to decode in base64 - Stack Overflow
python 3.x - Serialize in JSON a base64 encoded data - Stack Overflow
Considering that you have loaded the json data into an object. You can then try the following:
import json
import base64
json_obj = {
"equno": "229151246954324320",
"data": "CdwL703m6njjyJ7tdEXdRoYKKv49oatv9UWvdLONpWgd407fakXNfbt18j+qKb/toYgHZvj34ig7iu4XN7BfrqTf/wNsIdVNJ67c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zgRwrNoY0GJzeocxZlAa5f5ANu7OHltqpSTAZ0PzVCopG1NgwaQEpS08mVAtgXo7jq34VejdNuHiTo+/ht3Dn+C+WzKXHZIABkhHjGg1Bv4hJHuLXIpQjIE0xwQo2UcTmcAYvrGO6FcHZz+eRUmJyrtsJczwZK7nimfgJ6T/iuggPVwyn9pifU9VA=="
}
print(base64.b64decode(json_obj["data"]))
Here's a way to do that in Python:
import base64
import json
with open("sample_data.json") as f:
text = f.read()
d = json.loads(text)
data = base64.b64decode(d["data"])
The variable data now contains the decoded content of the relevant item in the json file.
You are passing in your whole list, which itself contains more lists:
>>> response_i['objcontent'][0]['rowvalues']
[['AAAAAAAALkA=', 'AAAAAADgU8A=', 'AAAAAAAA8D8=', '7HitYA'], ['AAAAAAAALkA=', 'AAAAAADgU8A=', 'AAAAAAAACEA=', '7HitYA'], ['AAAAAAAALkA=', 'AAAAAADgU8A=', 'AAAAAAAAFEA=', '7HitYA']]
You'd need to decode each separate entry, not the whole list:
for response_i in response['response']:
for row in response_i['objcontent'][0]['rowvalues']:
for encoded_obj in row[:-1]:
decoded = base64.b64decode(encoded_obj)
Note that I ignore the last value in the row, that's not a Base64 value.
Your next problem is that your Base64 data doesn't contain enough bytes to hold 12 float values (each 8 bytes, so you'd need 96 bytes). Since they are 8 bytes each, you probably have just one floating point value in each string.
So you'd want to decode just the one float:
floating_point_value = struct.unpack('d', decoded)[0]
Demo:
>>> for response_i in response['response']:
... for row in response_i['objcontent'][0]['rowvalues']:
... for encoded_obj in row[:-1]:
... decoded = base64.b64decode(encoded_obj)
... print(struct.unpack('d', decoded)[0])
...
15.0
-79.5
1.0
15.0
-79.5
3.0
15.0
-79.5
5.0
Your json seems to be malformed, there are more opening [ than closing ]. So I can't give you an exact breakdown.
In any event, the value that matches the key 'rowvalues' is a list of lists [ [], [], [] ]. So you need to break it down accordingly to pass the correct values to the decoder
for value_list in response_i['objcontent'][0]['rowvalues']:
for value in value_list:
# enter your decode code here
You must be careful about the datatypes.
If you read a binary image, you get bytes. If you encode these bytes in base64, you get ... bytes again! (see documentation on b64encode)
json can't handle raw bytes, that's why you get the error.
I have just written some example, with comments, I hope it helps:
from base64 import b64encode
from json import dumps
ENCODING = 'utf-8'
IMAGE_NAME = 'spam.jpg'
JSON_NAME = 'output.json'
# first: reading the binary stuff
# note the 'rb' flag
# result: bytes
with open(IMAGE_NAME, 'rb') as open_file:
byte_content = open_file.read()
# second: base64 encode read data
# result: bytes (again)
base64_bytes = b64encode(byte_content)
# third: decode these bytes to text
# result: string (in utf-8)
base64_string = base64_bytes.decode(ENCODING)
# optional: doing stuff with the data
# result here: some dict
raw_data = {IMAGE_NAME: base64_string}
# now: encoding the data to json
# result: string
json_data = dumps(raw_data, indent=2)
# finally: writing the json string to disk
# note the 'w' flag, no 'b' needed as we deal with text here
with open(JSON_NAME, 'w') as another_open_file:
another_open_file.write(json_data)
Alternative solution would be encoding stuff on the fly with a custom encoder:
import json
from base64 import b64encode
class Base64Encoder(json.JSONEncoder):
# pylint: disable=method-hidden
def default(self, o):
if isinstance(o, bytes):
return b64encode(o).decode()
return json.JSONEncoder.default(self, o)
Having that defined you can do:
m = {'key': b'\x9c\x13\xff\x00'}
json.dumps(m, cls=Base64Encoder)
It will produce:
'{"key": "nBP/AA=="}'
base64.b64encode(data) will output an object in bytes
encoded = base64.b64encode(data).decode() converts it to string
after that you might need (quite common) to do url encoding to the string
from urllib.parse import urlencode
urlencode({"groupedImage": encoded})
If it is image which you want to send as http reponse you shouldn't be doing json.dumps , instead you can send raw bytes and receive it.
However if you still want to do you'll need to change to json.dumps(str(retObj))