How to Use the Redis to Store Json Data

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

DBAs, developers, and technical specialists must be able to store and manipulate a variety of data types such as strings, hashes, and sets. ReJSON’s data management flexibility stands out as a main primary feature because it supports these plus many other JavaScript Object Notation (JSON) values. Learn how to use Redis store JSON data methods and discover why this capability satisfies many database users’ needs.

Prerequisites

  • Download and install Redis if you don’t already have it on your OS.

  • Use the redis-cli --version command to confirm that you also have the interactive Redis command line interface.

What Is Redis Json

The Redis module called RedisJSON enables users to store, fetch, and update data (values in JSON) taken from Redis documents. The native data type it applies is the Standard ECMA-404, the Standard JSON Data Interchange Syntax.

Using the Redis to Store Json Data

In the RedisJSON module, there are two Redis store JSON methods to use:

  • Use Raw strings in JSON from Redis documents (keys)

Documents, when stored, contain a converted serialized form, so you can use this method to Redis store JSON.

>NOTE: When using the Raw strings storage method, it may also be possible to decode the strings using a version of the LUA programming language with a parser you create.

  • Hash keys where the data is decomposed

This method makes key-value pairs from the deserialized data.

Raw Json String Keys

  • Try this example showing how to store Raw JSON string keys with the command SET like this:
1
2
3
4
127.0.0.1:6379> SET jsondata '{"str": "example", "int": 20}'
OK
127.0.0.1:6379> GET jsondata
"{"str": "example", "int": 20}"

Since the Redis store JSON data is serialized, it is automatically readable. Therefore, reading the data is all you need to do. If you want to fetch it, you won’t have to do anything else to accomplish that task. This is great for caching binary large objects (BLOBs).

>NOTE: Being unable to access individual elements without having to read the complete BLOB is not possible and this is a limitation to using Raw JSON strings in RedisJSON.

Decomposed Hash Keys

Here is how you can use Redis hash keys and a decomposed JSON value. Furthermore, with decomposed hash keys, you can mimic objects to a modeling object.

View this hash key decomposed example:

1
2
3
4
127.0.0.1:6379> HSET decomposed str example
(integer) 1
127.0.0.1:6379> HSET decomposed int 20
(integer) 1

All hash elements are in one time which is O(1) constant time and accessible when they are decomposed in Redis.

>NOTE: Redis Hash is limited to data types that consist of string numbers. In addition, nesting isn’t possible, nor does this Redis store JSON hash key decomposed method give users the ability to implement LUA.

The Redis Rejson Basic Set and Get Command

The RedisJSON (ReJSON)is short for Redis and JSON. The RedisJSON module has many features that make it easy to use:

  • JSON values for the keys must be valid such as objects, arrays, or scalars. They also can be nested, but they don’t have to be.

  • The data is stored in binary format after it is decoded.

  • The syntax resembles a JSONPath and this makes elements accessible.

  • Atomic commands prevent language mixups because the commands are strongly-typed.

View this example of RedisJSON (in the code, we use ReJSON) that applies the JSON.SET and JSON.GET commands:

1
2
3
4
5
6
7
8
127.0.0.1:6379> JSON.SET scalar . '"Demo ReJSON"'
OK
127.0.0.1:6379> JSON.SET doc . '{"str": "example", "int": "20"}'
OK
127.0.0.1:6379> JSON.GET doc
"{"str": "example", "int": 20}"
127.0.0.1:6379> JSON.GET doc .str
"example"

In Redis, JSON has complete instinctual element support. This advantage makes it possible to quickly navigate to elements specified on a path.

>NOTE: Some limitations are present though. For example, it costs more to serialize back to a JSON value using the Redis store JSON method. In addition, it uses more memory compared to serialization.

Conclusion

The RedisJSON module is based on the JSON standard data type, so it is highly compatible with a variety of JavaScript elements and document types. What’s more, the advantages of incorporating the Redis store JSON methods outlined in this tutorial far outweigh any identified limitations. Databases are made to store records, but they also must be storehouses where fetching and updating records are just as simple to accomplish. RedisJSON does that and more. Starting today, make it a regular component of your project work.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.