JSON support for Redis

Discord Github

RedisJSON is a Redis module that provides JSON support in Redis. RedisJSON lets your store, update, and retrieve JSON values in Redis just as you would with any other Redis data type. RedisJSON also works seamlessly with RediSearch to let you index and query your JSON documents.

Primary features

  • Full support for the JSON standard
  • A JSONPath syntax for selecting/updating elements inside documents
  • Documents stored as binary data in a tree structure, allowing fast access to sub-elements
  • Typed atomic operations for all JSON values types

Using RedisJSON

To learn how to use RedisJSON, it's best to start with the Redis CLI. The following examples assume that you're connected to a Redis server with RedisJSON enabled.

With redis-cli

To following along, start redis-cli.

The first RedisJSON command to try is JSON.SET, which sets a Redis key with a JSON value. All JSON values can be used, for example a string:> JSON.SET foo $ '"bar"'
OK> JSON.GET foo $
"[\"bar\"]"> JSON.TYPE foo $
1) string

JSON.GET and JSON.TYPE do literally that regardless of the value's type, but you should really check out JSON.GET prettifying powers. Note how the commands are given the dollar sign character, i.e. $. This is the path to the value in the RedisJSON data type (in this case it just means the root). A couple more string operations:> JSON.STRLEN foo $
1) (integer) 3> JSON.STRAPPEND foo $ '"baz"'
1) (integer) 6> JSON.GET foo $

JSON.STRLEN tells you the length of the string, and you can append another string to it with JSON.STRAPPEND. Numbers can be incremented and multiplied:> JSON.SET num $ 0
"[1]"> JSON.NUMINCRBY num $ 1.5
"[2.5]"> JSON.NUMINCRBY num $ -0.75
"[1.75]"> JSON.NUMMULTBY num $ 24

Of course, a more interesting example would involve an array or maybe an object:> JSON.SET amoreinterestingexample $ '[ true, { "answer": 42 }, null ]'
OK> JSON.GET amoreinterestingexample $
"[[true,{\"answer\":42},null]]"> JSON.GET amoreinterestingexample $[1].answer
"[42]"> JSON.DEL amoreinterestingexample $[-1]
(integer) 1> JSON.GET amoreinterestingexample $

The handy JSON.DEL command deletes anything you tell it to. Arrays can be manipulated with a dedicated subset of RedisJSON commands:> JSON.SET arr $ []
1) (integer) 1> JSON.GET arr $
"[[0]]"> JSON.ARRINSERT arr $ 0 -2 -1
1) (integer) 3> JSON.GET arr $
"[[-2,-1,0]]"> JSON.ARRTRIM arr $ 1 1
1) (integer) 1> JSON.GET arr $
"[[-1]]"> JSON.ARRPOP arr $
1) "-1"> JSON.ARRPOP arr $
1) (nil)

And objects have their own commands too:> JSON.SET obj $ '{"name":"Leonard Cohen","lastSeen":1478476800,"loggedOut": true}'
1) (integer) 3> JSON.OBJKEYS obj $
1) 1) "name"
   2) "lastSeen"
   3) "loggedOut"

Python example

This code snippet shows how to use RedisJSON with raw Redis commands from Python with redis-py:

import redis
import json

data = {
    'foo': 'bar'

r = redis.Redis()
r.json().set('doc', '$', json.dumps(data))
reply = json.loads(r.json().get('doc', '$')[0])

Building on Ubuntu 20.04

The following packages are required to successfully build on Ubuntu 20.04:

sudo apt install build-essential llvm cmake libclang1 libclang-dev cargo

Then, run make or cargo build --release in the repository directory

Loading the module to Redis


We recommend you have Redis load the module during startup by adding the following to your redis.conf file:

loadmodule /path/to/module/target/release/

On Mac OS, if this module has been built as a dynamic library use:

loadmodule /path/to/module/target/release/librejson.dylib

In the above lines replace /path/to/module/ with the actual path to the module's library.

Alternatively, you can have Redis load the module using the following command line argument syntax:

~/$ redis-server --loadmodule ./target/release/

Lastly, you can also use the [MODULE LOAD](/commands/module-load) command. Note, however, that MODULE LOAD is a dangerous command and may be blocked/deprecated in the future due to security considerations.

Once the module has been loaded successfully, the Redis log should have lines similar to:


1877:M 23 Dec 02:02:59.725 # <RedisJSON> JSON data type for Redis - v1.0.0 [encver 0]
1877:M 23 Dec 02:02:59.725 * Module 'RedisJSON' loaded from <redacted>/src/


Commands Overview

Search/Indexing JSON documents

Searching and indexing JSON documents



Client Libraries

List of RedisJSON client libraries


Performance benchmarks

RedisJSON RAM Usage

Debugging memory consumption

Developer notes

Notes on debugging, testing and documentation