Working with JSON Documents
1. Setting JSON Documents
| Command | Description |
|---|---|
JSON.SET key path value [NX|XX] | Use $ for root |
JSON.SET user:42 $ '{"name":"Alice","age":30,"tags":["admin"]}'
2. Getting JSON Values
| Command | Options |
|---|---|
JSON.GET key [INDENT s] [NEWLINE n] [SPACE s] [path ...] | Multiple JSONPaths in one call |
JSON.MGET k1 k2 ... path | Same path across keys |
3. Deleting JSON Keys
| Command | Description |
|---|---|
JSON.DEL key [path] | Remove field or entire doc |
JSON.FORGET | Alias for JSON.DEL |
4. Using JSONPath Queries
| Expression | Match |
|---|---|
$ | Root |
$.field | Direct child |
$..field | Recursive descent |
$.arr[0] | Array index |
$.arr[*] | All elements |
$.arr[?(@.age > 18)] | Filter |
5. Handling Nested Objects
JSON.SET user:42 $.address '{"city":"NYC","zip":"10001"}'
JSON.GET user:42 $.address.city
| Pattern | Detail |
|---|---|
| Partial update | SET subtree path without rewriting whole doc |
6. Getting Object Keys
| Command | Returns |
|---|---|
JSON.OBJKEYS key [path] | Keys at object path |
7. Getting Object Length
| Command | Returns |
|---|---|
JSON.OBJLEN key [path] | Key count of object |
8. Incrementing Numeric Values
| Command | Description |
|---|---|
JSON.NUMINCRBY key path n | Atomic numeric increment |
JSON.NUMMULTBY key path n | Atomic multiply DEPRECATED |
9. Appending to Strings
| Command | Description |
|---|---|
JSON.STRAPPEND key [path] "value" | Append to JSON string |
10. Getting String Length
| Command | Returns |
|---|---|
JSON.STRLEN key [path] | Character length |
11. Appending to Arrays
| Command | Description |
|---|---|
JSON.ARRAPPEND key path v [v ...] | Append elements; returns new length |
12. Getting Array Length
| Command | Returns |
|---|---|
JSON.ARRLEN key [path] | Element count |
13. Inserting Array Elements
| Command | Description |
|---|---|
JSON.ARRINSERT key path index v [v ...] | Insert at index |
JSON.ARRINDEX key path scalar [start [stop]] | Find index of scalar |
14. Popping Array Elements
| Command | Description |
|---|---|
JSON.ARRPOP key [path [index]] | Remove and return; default last |
15. Trimming Arrays
| Command | Description |
|---|---|
JSON.ARRTRIM key path start stop | Keep only range |