Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 535
Guido Avatar asked Jul 22 '26 02:07

Guido


1 Answers

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.

like image 103
Wayne Werner Avatar answered Jul 24 '26 15:07

Wayne Werner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!