Databases 6 min read

Mastering JSON Storage in Redis with ReJSON: A Hands‑On Guide

Learn how to store, retrieve, and manipulate JSON data in Redis using the ReJSON module, covering basic commands, advanced field operations, numeric updates, and step‑by‑step installation on Redis 4.0+, with practical code examples and configuration instructions.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Mastering JSON Storage in Redis with ReJSON: A Hands‑On Guide

1. Introduction

Redis supports various data types such as String, Hash, Set, and List. When we need to store JSON data in Redis, using String or Hash is inconvenient, so the ReJSON module provides native JSON storage.

2. Example

2.1 Basic Operations

127.0.0.1:6379> JSON.SET object . '{"foo": "bar", "ans": 42}'
OK
127.0.0.1:6379> JSON.GET object
"{\"foo\":\"bar\", \"ans\":42}"

The command JSON.SET sets a JSON document at the root (.) of the key object. JSON.GET retrieves the JSON value.

2.2 JSON Path Operations

Get a field value:

127.0.0.1:6379> JSON.GET object .ans
"42"

Set a field value:

127.0.0.1:6379> json.set object .name '"bill"'
OK
127.0.0.1:6379> json.get object
"{\"foo\":\"bar\",\"ans\":42,\"hi\":\"hello\",\"name\":\"bill\"}"

Delete a field:

127.0.0.1:6379> json.del object .name
(integer) 1
127.0.0.1:6379> json.get object
"{\"foo\":\"bar\",\"ans\":42,\"hi\":\"hello\"}"

Numeric operations on the ans field:

127.0.0.1:6379> json.numincrby object .ans 3
"45"
127.0.0.1:6379> json.get object
"{\"foo\":\"bar\",\"ans\":45,\"hi\":\"hello\"}"
127.0.0.1:6379> json.nummultby object .ans 2
"90"
127.0.0.1:6379> json.get object
"{\"foo\":\"bar\",\"ans\":90,\"hi\":\"hello\"}"

3. Installation

ReJSON requires Redis 4.0 or later.

3.1 Installation Steps

Install Redis 4.0

Install system dependencies

Install the ReJSON module

Load the ReJSON module when starting Redis

3.2 Detailed Installation Process

Install Redis 4.0:

wget https://github.com/antirez/redis/archive/4.0-rc2.tar.gz
tar xzf 4.0-rc2.tar.gz
cd redis-4.0-rc2/
make

Install dependencies (CentOS example, Ubuntu uses apt-get install build-essential): yum groupinstall "Development Tools" Install CMake:

# wget https://cmake.org/files/v3.8/cmake-3.8.0-rc3.tar.gz
# tar -xzvf cmake-2.8.11.2.tar.gz
# cd cmake-2.8.11.2
# ./bootstrap
# make
# make install

Install ReJSON module:

git clone https://github.com/RedisLabsModules/rejson.git
cd rejson
./bootstrap.sh
cmake --build build --target rejson

After building, rejson.so appears in the lib directory. Start Redis with the module:

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

Startup logs will show the ReJSON module information, and JSON commands become available.

5. Summary

ReJSON enables convenient storage and manipulation of JSON data in Redis, demonstrating the power of Redis modules and paving the way for future extensions.

Project URL:

https://redislabsmodules.github.io/rejson/
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseJSONInstallationReJSON
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.