如何在redis 4.0中设置json个对象?

How to set json objects in redis 4.0?

redis-py 4.0 开始,支持 json。我在 Windows 11 上使用 redis-py 版本 4.0.2。我在 docker 中有一个 Redis 服务器 运行 利用 redislabs/rejson:latest.

我正在尝试通过以下方式加载 json 对象 -

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)

在测试中是如何完成的 - https://github.com/redis/redis-py/blob/e8bcdcb97b8c915e2bb52353aeda17c00e1be215/tests/test_json.py#L19

但是我收到这个错误 -

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

我错过了什么?

您的 json 中有一个 b 前缀,代表字节。

只需从 b"some": 中删除 'b',它将按预期工作:

d = {"hello": "world", "some": "value"}