rejson (Deprecated: use redis-py for RedisJSON features)
rejson-py is a Python client library for RedisJSON, a Redis module that implements the JSON data type. It provides an extended interface to redis-py, offering on-the-fly serialization and deserialization of JSON objects. As of `redis-py` version 4.0.0, this library is deprecated, and its functionalities have been integrated directly into the `redis-py` client. The `rejson` package is currently at version 0.5.6 and has a very slow release cadence, with the last update in November 2021.
Common errors
-
ImportError: No module named 'rejson'
cause The 'rejson' Python package is not installed in your environment.fixRun `pip install rejson` to install the deprecated client, or `pip install redis` for the recommended `redis-py` client. -
redis.exceptions.ResponseError: unknown command `JSON.SET`
cause Your Redis server does not have the RedisJSON module loaded, or you are connecting to a Redis instance without the module.fixEnsure you are connecting to a Redis server that has the RedisJSON module installed and loaded. A simple way to test is to use `docker run -p 6379:6379 redislabs/rejson:latest` and configure your client to connect to `localhost:6379`. -
AttributeError: 'Redis' object has no attribute 'jsonset'
cause You are trying to use `rejson-py` client methods (like `jsonset`) on a standard `redis.Redis` object, likely after upgrading `redis-py` or attempting to use its native JSON features without the `rejson-py` client.fixIf you intend to use `rejson-py`, ensure you import `Client` from `rejson`. If you are using `redis-py` (v4.0.0+), access JSON commands via the `json()` method of the `Redis` client, e.g., `r = Redis(); r.json().set(...)`.
Warnings
- breaking The `rejson-py` library is deprecated since `redis-py` version 4.0.0. All its features for interacting with RedisJSON have been merged into the official `redis-py` client.
- gotcha `rejson-py` (and the `redis-py` JSON client) requires a Redis server with the RedisJSON module specifically loaded. Installing the Python client alone is not enough; the Redis server must be configured with the module.
- breaking RedisJSON server module (v2.6.9+) changed the default JSONPath root from `$` to `.` under RESP3. While `rejson-py` abstracts some of this, direct path usage might be affected.
Install
-
pip install rejson -
pip install redis
Imports
- Client
from redis import Redis
from rejson import Client, Path
- Path
from rejson import Client, Path
Quickstart
import os
from rejson import Client, Path
import json
# Ensure a RedisJSON server is running, e.g., via Docker:
# docker run -d -p 6379:6379 redislabs/rejson:latest
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
REDIS_PORT = int(os.environ.get('REDIS_PORT', 6379))
try:
# Using the deprecated rejson-py client
rj = Client(host=REDIS_HOST, port=REDIS_PORT)
obj = {
'name': 'Alice',
'age': 30,
'city': 'New York',
'hobbies': ['reading', 'hiking'],
'address': {'street': '123 Main St', 'zip': '10001'}
}
# Set a JSON object at the root path
rj.jsonset('user:1', Path.rootPath(), obj)
print(f"Set JSON for user:1: {obj}")
# Get a specific field
name = rj.jsonget('user:1', Path('.name'))
print(f"User's name: {name}")
# Update a field
rj.jsonset('user:1', Path('.age'), 31)
updated_age = rj.jsonget('user:1', Path('.age'))
print(f"Updated user's age: {updated_age}")
# Append to an array
rj.jsonarrappend('user:1', Path('.hobbies'), 'coding')
hobbies = rj.jsonget('user:1', Path('.hobbies'))
print(f"User's hobbies after appending: {hobbies}")
# --- Recommended modern approach using redis-py (v4.0.0+) ---
# from redis import Redis
# r = Redis(host=REDIS_HOST, port=REDIS_PORT)
# r.json().set('user:2', Path.root_path(), {'email': 'bob@example.com'})
# email = r.json().get('user:2', Path('.email'))
# print(f"User 2 email (via redis-py): {email}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure a Redis server with the ReJSON module is running and accessible.")