JSON

JSON support for Redis

Discord Github

The JSON capability of Redis Stack provides JavaScript Object Notation (JSON) support for Redis. It lets you store, update, and retrieve JSON values in a Redis database, similar to any other Redis data type. Redis JSON also works seamlessly with the Redis Query Engine to let you index and query JSON documents.

Primary features

  • Full support for the JSON standard
  • A JSONPath syntax for selecting/updating elements inside documents (see JSONPath syntax)
  • Documents stored as binary data in a tree structure, allowing fast access to sub-elements
  • Typed atomic operations for all JSON value types

Use Redis with JSON

The first JSON command to try is JSON.SET, which sets a Redis key with a JSON value. JSON.SET accepts all JSON value types. This example creates a JSON string:

Note how the commands include the dollar sign character $. This is the path to the value in the JSON document (in this case it just means the root).

Here are a few more string operations. JSON.STRLEN tells you the length of the string, and you can append another string to it with JSON.STRAPPEND.

Numbers can be incremented and multiplied:

Here's a more interesting example that includes JSON arrays and objects:

The JSON.DEL command deletes any JSON value you specify with the path parameter.

You can manipulate arrays with a dedicated subset of JSON commands:

JSON objects also have their own commands:

Format CLI output

The CLI has a raw output mode that lets you add formatting to the output from JSON.GET to make it more readable. To use this, run redis-cli with the --raw option and include formatting keywords such as INDENT, NEWLINE, and SPACE with JSON.GET:

$ redis-cli --raw
> JSON.GET obj INDENT "\t" NEWLINE "\n" SPACE " " $
[
	{
		"name": "Leonard Cohen",
		"lastSeen": 1478476800,
		"loggedOut": true
	}
]

Enable Redis JSON

Redis JSON is not available by default in the basic Redis server, so you should install Redis Stack or Redis Enterprise, both of which include JSON and other useful modules. See Install Redis Stack or Install Redis Enterprise for full installation instructions.

Limitation

A JSON value passed to a command can have a depth of up to 128. If you pass to a command a JSON value that contains an object or an array with a nesting level of more than 128, the command returns an error.

Further information

Read the other pages in this section to learn more about Redis JSON

RATE THIS PAGE
Back to top ↑