3

As of redis-py 4.0, json is supported. I am using redis-py version 4.0.2 on Windows 11. I have a redis server running in docker utilizing redislabs/rejson:latest.

I am attempting to load a json object via the following -

import redis
from redis.commands.json.path import Path
from redis import exceptions
from redis.commands.json.decoders import unstring, decode_list

r = redis.Redis()
d = {"hello": "world", b"some": "value"}
r.json().set("somekey", Path.rootPath(), d)

Just how it is done in the tests - https://github.com/redis/redis-py/blob/e8bcdcb97b8c915e2bb52353aeda17c00e1be215/tests/test_json.py#L19

However I am getting this error -

TypeError: keys must be str, int, float, bool or None, not bytes

What am I missing?

2 Answers 2

3

You have a b prefix in your json, which stands for bytes.

Just remove 'b' from b"some": and it would work as expected:

d = {"hello": "world", "some": "value"}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Interesting enough, they use that in the testcase github.com/redis/redis-py/blob/…
3

This will fix it (adding decode_keys=True to decode any binary keys):

r.json().set("somekey", Path.rootPath(), d, decode_keys=True)

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.