Skip to main content

Storing and Querying JSON documents using Redis Stack


Profile picture for Ajeet Raina
Author:
Ajeet Raina, Former Developer Growth Manager at Redis

Redis Stack is an extension of Redis that adds modern data models and processing engines to provide a complete developer experience. Redis Stack provides a simple and seamless way to access different data models such as full-text search, document store, graph, time series, and probabilistic data structures enabling developers to build any real-time data application.

In this tutorial, you will see how Redis Stack can help you in storing and querying JSON documents.

Step 1. Create a free Cloud account

Create your free Redis Enterprise Cloud account. Once you click on “Get Started”, you will receive an email with a link to activate your account and complete your signup process.

tip

For a limited time, use TIGER200 to get $200 credits on Redis Enterprise Cloud and try all the advanced capabilities!

🎉 Click here to sign up

Step 2. Create Your database

Choose your preferred cloud vendor. Select the region and then click "Let's start free" to create your free database automatically.

tip

If you want to create a custom database with your preferred name and type of Redis, click "Create a custom database" option shown in the image.

create database

Step 3. Verify the database details

You will be provided with Public endpoint URL and "Redis Stack" as the type of database with the list of features that comes by default.

verify database

Step 4. Using RedisInsight

RedisInsight is a visual tool that lets you do both GUI- and CLI-based interactions with your Redis database, and so much more when developing your Redis based application. It is a fully-featured pure Desktop GUI client that provides capabilities to design, develop and optimize your Redis application. It works with any cloud provider as long as you run it on a host with network access to your cloud-based Redis server. It makes it easy to discover cloud databases and configure connection details with a single click. It allows you to automatically add Redis Enterprise Software and Redis Enterprise Cloud databases.

Follow this link to install RedisInsight v2 on your local system. Assuming that you already have RedisInsight v2 installed on your MacOS, you can browse through the Applications and click "RedisInsight-v2" to bring up the Redis Desktop GUI tool.

Step 5. Enter Redis Enterprise Cloud details

Add the Redis Enterprise cloud database endpoint, port and password.

access redisinsight

Step 6. Verify the database under RedisInsight dashboard

database details

Step 7. Getting Started with Redis JSON

The following steps use some basic Redis JSON commands. You can run them from the Redis command-line interface (redis-cli) or use the CLI available in RedisInsight.

To interact with Redis JSON, you will most often use the JSON.SET and JSON.GET commands. Before using Redis JSON, you should familiarize yourself with its commands and syntax as detailed in the documentation: Redis JSON Commands.

Let’s go ahead and test drive some JSON-specific operations for setting and retrieving a Redis key with a JSON value:

  • Scalar
  • Objects (including nested objects)
  • Arrays of JSON objects
  • JSON nested objects

Scalar

Under Redis JSON, a key can contain any valid JSON value. It can be scalar, objects or arrays. JSON scalar is basically a string. You will have to use the JSON.SET command to set the JSON value. For new Redis keys the path must be the root, so you will use “.” path in the example below. For existing keys, when the entire path exists, the value that it contains is replaced with the JSON value. Here you will use JSON.SET to set the JSON scalar value to “Hello JSON!” Scalar will contain a string that holds “Hello JSON!”

Command:
JSON.SET greetings .  ' "Hello JSON!" '
Result:
OK

Use JSON.GET to return the value at path in JSON serialized form:

Command:
JSON.GET greetings
Result:
"\"Hello JSON!\""

Objects

Let’s look at a 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 a colon. Each entry (key-value pair) is separated by a comma. The { (curly brace) represents the JSON object:

{
"employee": {
"name": "alpha",
"age": 40,
"married": true
}
}

Here is the command to insert JSON data into Redis:

Command:
JSON.SET employee_profile $ '{ "employee": { "name": "alpha", "age": 40,"married": true }  } '
note

Please note that the above command works for 2.0+ release of Redis JSON. If you are using the older version of Redis JSON, you can replace "$" with "."

Result:
"OK"

The subcommands below change 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:

Command:
JSON.GET employee_profile
Result:
"{\"employee\":{\"name\":\"alpha\",\"age\":40,\"married\":true}}"

Retrieving a part of JSON document

You can also retrieve a part of the JSON document from Redis. In the example below, “.ans” can be passed in the commandline to retrieve the value 4:

Command:
JSON.SET object . '{"foo":"bar", "ans":"4" }'
Result:
"OK"
Command:
JSON.GET object
Result:
"{\"foo\":\"bar\",\"ans\":\"4\"}"
Command:
JSON.GET object .ans
Results:
"\"4\""

Retrieving the type of JSON data

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.

Command:
JSON.TYPE employee_profile
Result:
"Object"

JSON arrays of objects

The JSON array represents an ordered list of values. A JSON array can store multiple values, including strings, numbers, or objects. In JSON arrays, values must be separated by a comma. The [ (square bracket) represents the JSON array. Let’s look at a simple JSON array example with four objects:

{"employees":[
{"name":"Alpha", "email":"alpha@gmail.com", "age":23},
{"name":"Beta", "email":"beta@gmail.com", "age":28},
{"name":"Gamma", "email":"gamma@gmail.com", "age":33},
{"name":"Theta", "email":"theta@gmail.com", "age":41}
]}
Command:
JSON.SET testarray .  '{"employees":[         {"name":"Alpha", "email":"alpha@gmail.com", "age":23},         {"name":"Beta", "email":"beta@gmail.com", "age":28},       {"name":"Gamma", "email":"gamma@gmail.com", "age":33},         {"name":"Theta", "email":"theta@gmail.com", "age":41}    ]}   '
Result:
"OK"
Command:
JSON.GET testarray
Result:
"{\"employees\":[{\"name\":\"Alpha\",\"email\":\
alpha@gmail.com

\",\"age\":23},{\"name\":\"Beta\",\"email\":\"beta@gmail.com....

JSON nested objects

A JSON object can also have another object. Here is a simple example of a JSON object having another object nested in it:

Command:
>> JSON.SET employee_info . ' { "firstName": "Alpha",         "lastName": "K", "age": 23,        "address" : {            "streetAddress": "110 Fulbourn Road Cambridge",  "city": "San Francisco", "state": "California", "postalCode": "94016"  } } '
Command:
>> JSON.GET employee_info
Result:
"{\"firstName\":\"Alpha\",\"lastName\":\"K\",\"age\":23,\"address\":{\"streetAddress\":\"110 Fulbourn Road Cambridge\",\"city\":\"San Francisco\",\"state\":\"California\",\"postalCode\":\"94016\"}}"

Next Steps

Redis Launchpad