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 ?

3
  • How was the file before? lso have you tried json.load() intead? Commented Dec 6, 2016 at 22:29
  • It raises also an error:*** AttributeError: 'bytes' object has no attribute 'read' Commented Dec 7, 2016 at 7: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. Commented May 9, 2018 at 9:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.