pythonjsondictionaryredisredis-py

How to set dict as json object using redis-py?


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?


Solution

  • 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"}