As of redis-py 4.0.0 this library is deprecated. It's features have been merged into redis-py. Please either install it from pypy or the repo.
rejson-py is a package that allows storing, updating and querying objects as JSON documents in a Redis database that is extended with the ReJSON module. The package extends redis-py's interface with ReJSON's API, and performs on-the-fly serialization/deserialization of objects to/from JSON.
bash
$ pip install rejson
virtualenv -v venv
pip install --user poetry
poetry install
tox runs all tests as its default target. Running tox by itself will run unit tests. Ensure you have a running redis, with the module loaded.
```python from rejson import Client, Path
rj = Client(host='localhost', port=6379, decode_responses=True)
# Set the key obj
to some object
obj = {
'answer': 42,
'arr': [None, True, 3.14],
'truth': {
'coord': 'out there'
}
}
rj.jsonset('obj', Path.rootPath(), obj)
# Get something print 'Is there anybody... {}?'.format( rj.jsonget('obj', Path('.truth.coord')) )
# Delete something (or perhaps nothing), append something and pop it rj.jsondel('obj', Path('.arr[0]')) rj.jsonarrappend('obj', Path('.arr'), 'something') print '{} popped!'.format(rj.jsonarrpop('obj', Path('.arr')))
# Update something else rj.jsonset('obj', Path('.answer'), 2.17)
# And use just like the regular redis-py client jp = rj.pipeline() jp.set('foo', 'bar') jp.jsonset('baz', Path.rootPath(), 'qaz') jp.execute()
# If you use non-ascii character in your JSON data, you can add the no_escape flag to JSON.GET command obj_non_ascii = { 'non_ascii_string': 'hyvää' } rj.jsonset('non-ascii', Path.rootPath(), obj_non_ascii) print '{} is a non-ascii string'.format(rj.jsonget('non-ascii', Path('.non_ascii_string'), no_escape=True)) ```
rejson-py uses Python's json. The client can be set to use custom encoders/decoders at creation, or by calling explicitly the setEncoder() and setDecoder() methods, respectively.
The following shows how to use this for a custom class that's stored as a JSON string for example:
```python
from json import JSONEncoder, JSONDecoder from rejson import Client
class CustomClass(object): "Some non-JSON-serializable" def init(self, s=None): if s is not None: # deserialize the instance from the serialization if s.startswith('CustomClass:'): ... else: raise Exception('unknown format') else: # initialize the instance ...
def __str__(self):
_str = 'CustomClass:'
# append the instance's state to the serialization
...
return _str
...
class CustomEncoder(JSONEncoder): "A custom encoder for the custom class" def default(self, obj): if isinstance(obj, CustomClass): return str(obj) return json.JSONEncoder.encode(self, obj)
class TestDecoder(JSONDecoder): "A custom decoder for the custom class" def decode(self, obj): d = json.JSONDecoder.decode(self, obj) if isinstance(d, basestring) and d.startswith('CustomClass:'): return CustomClass(d) return d
# Create a new instance of CustomClass obj = CustomClass()
# Create a new client with the custom encoder and decoder rj = Client(encoder=CustomEncoder(), decoder=CustomDecoder())
# Store the object rj.jsonset('custom', Path.rootPath(), obj))
# Retrieve it obj = rj.jsonget('custom', Path.rootPath()) ```
As rejson-py exposes the same methods as redis-py, it can be used as a drop-in replacement. On top of Redis' core commands, the client also adds ReJSON's vocabulary and a couple of helper methods. These are documented in the API.md file, which can be generated by running:
bash
$ python gendoc rejson > API.md
For complete documentation about ReJSON's commands, refer to ReJSON's website.
``` obj = { "id": 1, "uname": "abc" }
rj.jsonset('key1', Path.rootPath(), obj) ```
TestDecoder
to CustomDecoder
Ola, Estou tentando Utilizar o REJSON no python, ja iniciei utilizando o pip install rejson, instala normal, porem sempre me retorna esse erro, que nao esta encontrando o comando JSON.SET.. o que poderia ser ? estou rodando o servidor redis no localhost do windows..
File "d:\RAFA\WHATSAPP\whatsapp-bot\redis2.py", line 7, in <module>
r.execute_command('JSON.SET',"dic")
File "C:\Users\hto-r\AppData\Local\Programs\Python\Python39\lib\site-packages\redis\client.py", line 901, in execute_command
return self.parse_response(conn, command_name, **options)
File "C:\Users\hto-r\AppData\Local\Programs\Python\Python39\lib\site-packages\redis\client.py", line 915, in parse_response
response = connection.read_response()
File "C:\Users\hto-r\AppData\Local\Programs\Python\Python39\lib\site-packages\redis\connection.py", line 756, in read_response
raise response
redis.exceptions.ResponseError: unknown command 'JSON.SET'
Is rejon support batch upsert or bulk load?
for reference: https://docs.couchbase.com/python-sdk/2.5/batching-operations.html#asynchronous-batching
I see a key pointing to list of json :
python
redis_json_client.jsonset('customers', Path.rootPath(), { "customers": []})
````
example of insreted json
customer_json:
{
"customer_status": "Active",
"first_name": "Leonard",
"last_name": "Cohen",
"customer_no": 1
}
So now I append each of the json as following:
redis_json_client.jsonarrappend('customers', Path('.customers'), customer_json)
```
I'd like to preform a search finding specific customer_no
I tried something like:
customer_redis = redis_json_client.jsonget('customers', Path('customer_no'),370471789)
Q:
1.The reaosn for creating a list of json is that i'd like to some category collection in redis
,i wonder how bad it effect the performance ,
I could also assign the json as :
key,customer_json instead of key,[customer_json]
2.How could i preform a search for specific json where key holds a list of json as desribe above?
redis python json rejson