1

I totally new in Python and stuck with this error:

I am trying to encode an image and then convert it to json to upload into a no-sql db. But when I am trying to convert it to json I am getting this error:

"AttributeError: 'bytes' object has no attribute 'dict'"

Below is my python code:

import base64  
import json



def jsonDefault(object):
    return object.__dict__


with open("img123.png", "rb") as imageFile:  
    str = base64.b64encode(imageFile.read())  
    print(str)  
json_str = {'file_name':'img123','img_str':str}  
pytojson = json.dumps(json_str, default=jsonDefault)  
print(pytojson)
EsotericVoid
  • 2,306
  • 2
  • 16
  • 25
Kuntal
  • 21
  • 1
  • 5
  • What is your `jsonDefault` function supposed to do? – Błotosmętek Jul 23 '17 at 10:53
  • jsonDefault is used to make it JSON Serialized as earlier I was getting that error when using json.dumps(json_str). – Kuntal Jul 23 '17 at 11:32
  • You can't encapsulates a _**binary**_Object into `json` Object`. [Edit] your Question and explain why you would want to do that. – stovfl Jul 23 '17 at 17:20
  • Hi @Kuntal if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – EsotericVoid Jul 30 '17 at 05:30

1 Answers1

0

That happens because you are trying to access a property that a bytes object doesn't have (__dict__). I understand that you need to return a format that can be serialized to JSON.

This works for me, but I don't know if it's the decoding you desire:

def jsonDefault(o):
    return o.decode('utf-8')

See TypeError: b'1' is not JSON serializable as well.

EsotericVoid
  • 2,306
  • 2
  • 16
  • 25
  • As I am new to Python, what can be effects of "decode" in this case if you please explain. – Kuntal Jul 23 '17 at 11:34
  • @Kuntal [`bytes.decode`](https://docs.python.org/3/library/stdtypes.html#bytes.decode) is used to return a string decoded from the given bytes. The problem is that you can't serialize a bytes object with JSON, so you need to transform it in some way. A string representation of the image is what you wanted, right? – EsotericVoid Jul 23 '17 at 11:55
  • I want to store an image in my Cloudant Database. I think only way to do that is to store the encoded string of the image as json. I am more than happy to try your suggestion .One question, could I face any problem if I try to create the image from this decoded string again ? – Kuntal Jul 23 '17 at 17:54
  • @Kuntal have you tried for yourself to restore the image from the encoding? You should be able to use `base64.base64decode` to recover the image from the decoding. Please remember to mark the answer as accepted if it helped you. – EsotericVoid Jul 23 '17 at 21:46
  • 1
    so far your solution works as a charm.Thanks a lot. I will try to get it back to image. – Kuntal Jul 26 '17 at 18:26