pythonjsonredisbinascii

json.loads and Redis in python 3.5


I created a JSON object with json.dumps() and RPUSH(ed) it in a redis list. When getting back the JSON with LRANGE ( redis.lrange() ) I receive a binary string

 b'{"si":"00:ff" ...

So json.loads() raises an error: *** TypeError: the JSON object must be str, not 'bytes' How should I revert to ascii ?


Solution

  • In general you want to remember the acronym BADTIE:

    Bytes
    Are
    Decoded
    Text
    Is
    Encoded
    

    If you have bytes, you run my_bytes.decode() to get text.

    If you have text, you run my_text.encode() to get bytes. You can also specify the encoding if you know it, but it has a sensible default.