How to cache JSON data in Redis with Java

JRedisJSON is a Java client library for RedisJSON. This client provides access to RedisJSON's Redis API, and provides back-and-forth serialization between Java's and its objects. This client supports the core RedisJSON commands at the moment.

Follow the below steps to get started with RedisJSON and Java:

Step 1. Run RedisMod Docker container#

This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis Lab.

docker run -d -p 6379:6379 redislabs/redismod:latest

Step 2. Clone the repository#

git clone

Step 3. Install Maven#

If you have already installed Maven, then it will show that maven 3.8.1 is already installed and up-to-date. If not, then run the below command:

brew install mvn

Step 4. Create a file#

Copy the below content and save a file name

import redis.clients.jedis.Jedis;
import com.redislabs.modules.rejson.JReJSON;
// First get a connection
JReJSON client = new JReJSON("localhost", 6379);
// Setting a Redis key name _foo_ to the string _"bar"_, and reading it back
client.set("foo", "bar");
String s0 = (String) client.get("foo");
// Omitting the path (usually) defaults to the root path, so the call above to
// `get()` and the following ones // are basically interchangeable
String s1 = (String) client.get("foo", new Path("."));
String s2 = (String) client.get("foo", Path.ROOT_PATH);
// Any Gson-able object can be set and updated
client.set("obj", new Object()); // just an empty object
client.set("obj", null, new Path(".zilch"));
Path p = new Path(".whatevs");
client.set("obj", true, p);
client.set("obj", 42, p);
client.del("obj", p); // back to almost nothing

Step 5. Run the maven executables#

Use the "clean" command to delete all previously compiled Java .class files and resources (like .properties) in your project. Let your build start from a clean slate. Also, you can pass "install" to let it compile, test & package your Java project and even install/copy your built .jar/.war file into your local Maven repository.

mvn clean install -Dmaven.test.skip=true
1628854852.903490 [0] "JSON.SET" "obj" "." "{\"str\":\"string\",\"bTrue\":true}"
1628854852.905266 [0] "JSON.GET" "obj" "bTrue" "str"
1628854852.909629 [0] "FLUSHDB"
1628854852.911907 [0] "JSON.SET" "foobar" "." "{\"foo\":\"bar\"}"
1628854852.913345 [0] "JSON.TYPE" "foobar" ".foo[1]"
1628854852.918301 [0] "FLUSHDB"
1628854852.920702 [0] "JSON.SET" "obj" "." "{\"str\":\"string\",\"bTrue\":true}"
1628854852.922083 [0] "JSON.SET" "obj" ".none" "\"strangle\"" "XX"
1628854852.926862 [0] "FLUSHDB"
1628854852.930541 [0] "JSON.SET" "test" "." "\"foo\""
1628854852.932374 [0] "JSON.GET" "test" ".bar"
1628854852.937640 [0] "FLUSHDB"
1628854852.940467 [0] "JSON.SET" "obj" "." "{\"str\":\"string\",\"bTrue\":true}"
1628854852.941928 [0] "JSON.SET" "obj" ".none" "\"strangle\"" "NX"
1628854852.943441 [0] "JSON.GET" "obj" ".none"