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.
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/
makeInstall 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 installInstall ReJSON module:
git clone https://github.com/RedisLabsModules/rejson.git
cd rejson
./bootstrap.sh
cmake --build build --target rejsonAfter building, rejson.so appears in the lib directory. Start Redis with the module:
redis-server --loadmodule /path/to/module/rejson.soStartup 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/Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
