JSON

JSON support for Redis

Discord Github

The JSON capability of Redis Stack provides JavaScript Object Notation (JSON) support for Redis. It lets you store, update, and retrieve JSON values in a Redis database, similar to any other Redis data type. Redis JSON also works seamlessly with Search and Query to let you index and query JSON documents.

Primary features

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

Use Redis JSON

The first JSON command to try is JSON.SET, which sets a Redis key with a JSON value. JSON.SET accepts all JSON value types. This example creates a JSON string:

Note how the commands include the dollar sign character $. This is the path to the value in the JSON document (in this case it just means the root).

Here are a few more string operations. 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:

Here's a more interesting example that includes JSON arrays and objects:

The JSON.DEL command deletes any JSON value you specify with the path parameter.

You can manipulate arrays with a dedicated subset of JSON commands:

JSON objects also have their own commands:

Format CLI output

The CLI has a raw output mode that lets you add formatting to the output from JSON.GET to make it more readable. To use this, run redis-cli with the --raw option and include formatting keywords such as INDENT, NEWLINE, and SPACE with JSON.GET:

$ redis-cli --raw
> JSON.GET obj INDENT "\t" NEWLINE "\n" SPACE " " $
[
	{
		"name": "Leonard Cohen",
		"lastSeen": 1478476800,
		"loggedOut": true
	}
]

Enable Redis JSON

Redis JSON is implemented as a module that extends the basic Redis server. Redis Stack and Redis Enterprise include this module by default but you can also load it dynamically if it isn't already available. The sections below the different ways you can access Redis JSON.

Run with Docker

To run RedisJSON with Docker, use the redis-stack-server Docker image:

$ docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest

For more information about running Redis Stack in a Docker container, see Run Redis Stack on Docker.

Download binaries

To download and run the RedisJSON module that provides the JSON data structure from a precompiled binary:

  1. Download a precompiled version from the Redis download center.

  2. Load the module it in Redis

    $ redis-server --loadmodule /path/to/module/src/rejson.so
    

Build from source

To build RedisJSON from the source code:

  1. Clone the repository (make sure you include the --recursive option to properly clone submodules):

    $ git clone --recursive https://github.com/RedisJSON/RedisJSON.git
    $ cd RedisJSON
    
  2. Install dependencies:

    $ ./sbin/setup
    
  3. Build:

    $ make build
    

Load the module to Redis

Requirements:

Generally, it is best to run the latest Redis version.

If your OS has a Redis 6.x package or later, you can install it using the OS package manager.

Otherwise, you can invoke

$ ./deps/readies/bin/getredis

To load the RedisJSON module, use one of the following methods:

Makefile recipe

Run Redis with RedisJSON:

$ make run

Configuration file

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

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

On Mac OS, if this module was built as a dynamic library, run:

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

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

Command-line option

You can also have Redis load the module using the following command-line argument syntax:

$ redis-server --loadmodule /path/to/module/librejson.so

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

MODULE LOAD command

You can also use the MODULE LOAD command to load RedisJSON. Note that MODULE LOAD is a dangerous command and may be blocked/deprecated in the future due to security considerations.

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

...
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> version: 20009 git sha: d8d4b19 branch: HEAD
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> Exported RedisJSON_V1 API
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> Enabled diskless replication
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> Created new data type 'ReJSON-RL'
9:M 11 Aug 2022 16:24:06.701 * Module 'ReJSON' loaded from /opt/redis-stack/lib/rejson.so
...

Limitation

A JSON value passed to a command can have a depth of up to 128. If you pass to a command a JSON value that contains an object or an array with a nesting level of more than 128, the command returns an error.

Further information

Read the other pages in this section to learn more about Redis JSON

RATE THIS PAGE
Back to top ↑