3

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 ?

Guido
  • 634
  • 1
  • 8
  • 19
  • How was the file before? lso have you tried json.load() intead? – Rafael Rodrigo De Souza Dec 06 '16 at 22:29
  • It raises also an error:*** AttributeError: 'bytes' object has no attribute 'read' – Guido Dec 07 '16 at 07:38
  • I have the same problem but in Python3.6 is OK in local, and my server use py3.5 and get `TypeError: the JSON object must be str, not 'bytes'` If you don't want to update py just use `simplejson` replace built in `json`. But I don't why in detail....Sorry. – wyx May 09 '18 at 09:57

1 Answers1

11

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.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290