RedisJSON provides in-memory manipulation of JSON documents at high velocity and volume. With RedisJSON, you can natively store document data in a hierarchical, tree-like format to scale and query documents efficiently, significantly improving performance over storing and manipulating JSON with Lua and core Redis data structures.
In the next steps you will use some basic RedisJSON commands, you can run them from the redis-cli or using the CLI available in RedisInsight. (Click on “CLI” in the RedisInsight left menu).
To interact with RedisJSON you will most of the time use the JSON.SET and JSON.GET command. Before using RedisJSON, you should familiarize yourself with its commands and syntax as detailed in the commands reference document. Let’s go ahead and test drive the below JSON specific operations:
- Setting/Retrieving a Redis Key with a JSON value
- Scalar
- Objects(including Nested Objects)
- Arrays of JSON Objects
- JSON Nested Objects
Setting a Redis Key with JSON value
Use JSON.SET to set the JSON value at path in key. For new Redis keys the path must be the root. For existing keys, when the entire path exists, the value that it contains is replaced with the json value.
>> JSON.SET scalar . ' "Hello JSON!" '
"OK"
Use JSON.GET to return the value at path in JSON serialized form
>> JSON.GET scalar
"\"Hello JSON!\""
Let us look at JSON Object Example. A JSON object contains data in the form of a key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by colon. Each entry (key/value pair) is separated by comma.
The { (curly brace) represents the JSON object.
{
"employee": {
"name": "alpha",
"age": 40,
"married": true
}
}
Below is the command to insert JSON data into Redis:
>> JSON.SET employee_profile . '{ "employee": { "name": "alpha", "age": 40,"married": true } } '
"OK"
The following subcommands below changes the reply's format and are all set to the empty string by default: * INDENT sets the indentation string for nested levels * NEWLINE sets the string that's printed at the end of each line * SPACE sets the string that's put between a key and a value
>> >> JSON.GET employee_profile
"{\"employee\":{\"name\":\"alpha\",\"age\":40,\"married\":true}}"
If we want to retrieve a part of the JSON document from Redis, it is still possible. In the below example, “.ans” can be passed in the commandline to retrieve the value 4.
>> JSON.SET object . '{"foo":"bar", "ans":"4" }'
"OK"
>> JSON.GET object
"{\"foo\":\"bar\",\"ans\":\"4\"}"
>> JSON.GET object .ans
"\"4\""
JSON.TYPE reports the type of JSON value at path and path defaults to root if not provided. If the key or path do not exist, null is returned.
>> JSON.TYPE employee_profile
"Object"
The JSON array represents an ordered list of values. JSON array can store multiple values. It can store string, number, boolean or object in JSON array. In JSON arrays, values must be separated by comma. The [ (square bracket) represents JSON array.
Let's see a simple JSON array example having 4 objects.
{"employees":[
{"name":"Alpha", "email":"[email protected]", "age":23},
{"name":"Beta", "email":"[email protected]", "age":28},
{"name":"Gamma", "email":"[email protected]", "age":33},
{"name":"Theta", "email":"[email protected]", "age":41}
]}
>> JSON.SET testarray . ' {"employees":[ {"name":"Alpha", "email":"[email protected]", "age":23}, {"name":"Beta", "email":"[email protected]", "age":28}, {"name":"Gamma", "email":"[email protected]", "age":33}, {"name":”Theta", "email":"[email protected]", "age":41} ]} '
"OK"
>> JSON.GET testarray
"{\"employees\":[{\"name\":\"Alpha\",\"email\":\[email protected]\",\"age\":23},{\"name\":\"Beta\",\"email\":\"[email protected]....
It is possible that a JSON object can have another object too. Let's see a simple example of a JSON object having another object.
>> JSON.SET employee_info . ' { "firstName": "Alpha", "lastName": "K", "age": 23, "address" : { "streetAddress": "110 Fulbourn Road Cambridge", "city": "San Francisco", "state": "California", "postalCode": "94016" } } '
"OK"
>> JSON.GET employee_info
"{\"firstName\":\"Alpha\",\"lastName\":\"K\",\"age\":23,\"address\":{\"streetAddress\":\"110 Fulbourn Road Cambridge\",\"city\":\"San Francisco\",\"state\":\"California\",\"postalCode\":\"94016\"}}"
The dataset files are redis-cli scripts that you can import using the following command:
redis-cli -h localhost -p 6379 < employee.redis
Once done you can check some data:
% redis-cli -h localhost
localhost:6379> keys *
1) "employee_profile"
JSON.GET employee_profile
"{\"employee\":{\"name\":\"colin\",\"age\":23,\"single\":true}}"
127.0.0.1:6379>