2

I have a response object of byte type into convert to JSON and get data from it.
response = b'{"message": "test msg 3"}'

My code :

        try:
            response = response.decode('utf-8')
            LOGGER.info(response)
            data = json.load(response)
            message = data["message"]
        except AttributeError as err:
            message = "Message not delivered..."
            LOGGER.warning(err)

When response.decode('utf-8') executes it become {"message": "test msg 3"}

But Each time i am getting a error while executing json.load(response):

'str' object has no attribute 'read'

Can anyone point me why this happening ?

Solution :

Use json.loads() instead of json.load() . Because json.load() loads a file and json.loads() loads a String.

st_ahmed
  • 2,293
  • 1
  • 12
  • 8
  • 2
    [`json.load`](https://docs.python.org/3/library/json.html#json.load) tries to load from a file (or other readable object). [`json.loads`](https://docs.python.org/3/library/json.html#json.loads) accepts a string. – khelwood Jan 29 '18 at 11:19
  • Thanks @khelwood – st_ahmed Jan 30 '18 at 14:05

0 Answers0