Updated February 17, 2023
Introduction to RedisJSON
The RedisJSON module provides redis support. RedisJSON, like the other data types in the redis database, stores and retrieves json values. It also works with redis search to query and index json documents. It fully supports the json standard. We can update and select elements using RedisJSON path syntax.
Key Takeaways
- This module provides support in Redis; this module adds json as a native json data type.
- This module will extend the client of node redis for including the functions which we have defined in RedisJSON command.
What is RedisJSON?
RedisJSON allows us to create indexes on json documents and uses real-time indexing, allowing us to quickly query the indexed documents. The indexes query our data at lightning speed, performing complex aggregations on the property’s numeric ranges and geographical distances. It will support full-text indexing as well as stemming-based queries.
It is providing a rich query language which was performing complex structured queries and text searches. The RedisJSON enterprise and enterprise cloud will offer to scale up and scale down the RedisJSON across the entire cluster. It is allowing us to grow the indexes in billions of documents and it is situated in a number of servers. The RedisJSON module is adding support for the data structure of json for the database of redis.
How to Install RedisJSON?
In the below example we are building the RedisJSON by using the source code as follows:
1. In the first step we are downloading the repository from git. We are building after downloading the repository. We are installing the RedisJSON by using git as follows.
Command:
git clone --recursive ….git
Output:
2. After downloading the package now in this step, we are installing the dependencies as follows.
Command:
# ./sbin/setup
Output:
3. After installing the dependencies now in this step we are building the same by using the make build command as follows.
Command:
# make build
Output:
4. After installing the dependencies now in this step we are loading the module of redis as follows.
Command:
# ./deps/readies/bin/getredis
Output:
5. After loading the module now in this step we are running the module by using json as follows.
Code:
# make run
Output:
Uses of RedisJSON
We need to start the redis server by using json module. In the below example, we can see that we have started the redis server by using json module.
After starting the redis by using json module now in this step we are login into the redis cli as follows.
Command:
# redis-cli
Output:
After login into the redis cli now in this step we are using the JSON command to use in redis. In the below example, we can see that we are using set command with json as follows. We are defining color using json set command as follows.
Command:
JSON.SET color $ '"BLUE"'
Output:
In the below example, we can see that we are using the get command with json as follows. We are retrieving the value which we have set using json set command.
Command:
JSON.GET color $
Output:
After defining the get command now in the below example we are defining the type of value which we have passed in the set method.
Command:
JSON.TYPE color $
Output:
Below we are using string operation on value which we have provided with the set command, in the below example we are using STRLEN, STRAPPEND, and GET commands as follows.
Command:
JSON.STRLEN color $
JSON.STRAPPEND color $ '" (Red color)"'
JSON.GET color $
Output:
RedisJSON Types
The type of json is defined as the type we are using to use the value or to retrieve the value. We defined the RedisJSON types with the json type command. In the following example, we define the value in integer format, so the type of RedisJSON is as follows. Because we defined an integer value, we can see that the type of value is an integer.
Command:
JSON.SET number $ '15'
JSON.TYPE number $
Output:
In the following example, we define the value in string format, so the type of RedisJSON is as follows. Because we defined an integer value, we can see that the type of value is string.
Command:
JSON.SET col $ '"White"'
JSON.TYPE col $
Output:
If we define a type as a floating point number, RedisJSON will display it in number formats. In the following example, we defined the value as 10.5 as follows.
Command:
JSON.SET number $ '10.5'
JSON.TYPE number $
Output:
RedisJSON Path Syntax
RedisJSON will introduce path support in json. The json path query resolves the multiple locations into the document of JSON.
Syntax:
JSON.SET(COMMAND) KEY ‘$.val[“ “]’
Below is the syntax element of RedisJSON path as follows. It contains the element which we have defined in RedisJSON path.
- $ – This is defined as RedisJSON path is starting.
- [] or . (dot) – It will select the child element.
- .. – This is recursively defining the documents of JSON.
- * – This is used to return all the elements.
- [] – This is subsequent operations or defining the array elements.
- [,] – This is used to define the multiple elements in it.
- [start:end:step] – This will define the array where it is starting and ending.
- ?() – This defines filter for object array json.
- () – It will define the expression of the script.
- @ – It will define the current element.
Examples of RedisJSON
Given below are the examples mentioned:
Example #1
The below example shows by using JSON we are multiplying the number as follows.
Command:
JSON.SET redis_num $ 10
JSON.NUMINCRBY redis_num $ 15
JSON.NUMINCRBY redis_num $ 10.5
JSON.NUMINCRBY redis_num $ -10.5
JSON.NUMINCRBY redis_num $ 25
Output:
Example #2
In the below example, we are using set and get method with redis as follows.
Command:
JSON.SET redis_color $ '"Black"'
JSON.SET redis_color
Output:
Example #3
In the below example, we are setting the object. In json objects will contain their own commands as follows.
Command:
JSON.SET obj $ '{"stud_name":"ABC","stud_no":1,"stud_addr": "Pune"}'
JSON.OBJLEN obj $
JSON.OBJKEYS obj $
Output:
Conclusion
RedisJSON enterprise and enterprise cloud will provide the ability to scale up and scale down across the entire cluster. The module provides support in redis. It stores, updates, and retrieves json values in the redis database in the same way that other data types in the redis database do.
Recommended Articles
This is a guide to RedisJSON. Here we discuss the introduction, how to install RedisJSON? types and examples respectively. You may also have a look at the following articles to learn more –