JSON.SET-&-JSON.GET
RedisJSON stores JSON documents natively. Query nested paths, update individual fields, and manipulate arrays without fetching the entire document.
You'll Learn
- Storing JSON with JSON.SET
- Querying paths with JSON.GET
- Updating nested fields
- Array and object manipulation
See Your Data, Not Terminal Text
Redimo visualizes every Redis data type beautifully. Edit inline, undo mistakes, stay safe in production.
1. What Is RedisJSON?
Normally, Redis stores JSON as a string - you GET the whole thing, parse it, modify it, and SET it back. RedisJSON (a Redis module) understands JSON structure and lets you operate on parts of it directly.
Without RedisJSON
- GET → Parse → Modify → Stringify → SET
- Always transfer entire document
- Race conditions on concurrent updates
With RedisJSON
- Update specific path atomically
- Transfer only what you need
- Native array/object operations
Availability
MODULE LIST to see if it's loaded. Cloud providers like Redis Cloud and some managed services include it.2. JSON.SET: Store Documents
JSON.SET stores a JSON value at a key and path. The root path is $ (or . for legacy syntax).
Basic JSON.SET
# Store a document at root
JSON.SET user:1001 $ '{"name":"John","email":"john@example.com","age":30}'
OK
# Retrieve it
JSON.GET user:1001
"{\"name\":\"John\",\"email\":\"john@example.com\",\"age\":30}"
# Pretty print
JSON.GET user:1001 INDENT " " NEWLINE "\n"
{
"name": "John",
"email": "john@example.com",
"age": 30
}SET Options
# Only set if key doesn't exist (NX)
JSON.SET user:1002 $ '{"name":"Jane"}' NX
OK # or (nil) if exists
# Only set if key exists (XX)
JSON.SET user:1001 $ '{"name":"John Updated"}' XX
OK # or (nil) if doesn't exist3. JSON.GET: Query Paths
Use JSONPath expressions to retrieve specific parts of a document without fetching everything.
Path Queries
# Get specific field
JSON.GET user:1001 $.name
"[\"John\"]"
# Get multiple fields
JSON.GET user:1001 $.name $.email
"{\"$.name\":[\"John\"],\"$.email\":[\"john@example.com\"]}"
# Nested paths
JSON.SET user:1001 $ '{"profile":{"settings":{"theme":"dark"}}}'
JSON.GET user:1001 $.profile.settings.theme
"[\"dark\"]"
# Array access
JSON.SET user:1001 $ '{"tags":["admin","vip","active"]}'
JSON.GET user:1001 $.tags[0]
"[\"admin\"]"
JSON.GET user:1001 $.tags[-1]
"[\"active\"]"JSONPath Syntax
$- Root of document$.field- Object field$.arr[0]- Array index (0-based)$.arr[-1]- Last element$.arr[*]- All elements$..field- Recursive descent
4. Updating Documents
Update specific fields without touching the rest of the document.
Field Updates
# Update a single field
JSON.SET user:1001 $.email '"newemail@example.com"'
OK
# Add a new field
JSON.SET user:1001 $.verified 'true'
OK
# Update nested field
JSON.SET user:1001 $.profile.settings.theme '"light"'
OK
# Delete a field
JSON.DEL user:1001 $.temporary_field
(integer) 1Numeric Operations
# Increment number
JSON.SET user:1001 $.points '100'
JSON.NUMINCRBY user:1001 $.points 10
"110"
# Multiply
JSON.NUMMULTBY user:1001 $.points 2
"220"
# Works on nested paths too
JSON.NUMINCRBY user:1001 $.stats.loginCount 15. Array Operations
RedisJSON has dedicated commands for array manipulation - append, insert, pop, trim.
Array Commands
# Setup
JSON.SET user:1001 $ '{"tags":["user"]}'
# Append to array
JSON.ARRAPPEND user:1001 $.tags '"vip"' '"premium"'
(integer) 3 # New length
# Insert at position
JSON.ARRINSERT user:1001 $.tags 1 '"early-adopter"'
(integer) 4 # ["user","early-adopter","vip","premium"]
# Get array length
JSON.ARRLEN user:1001 $.tags
(integer) 4
# Pop last element
JSON.ARRPOP user:1001 $.tags
"\"premium\""
# Pop from index
JSON.ARRPOP user:1001 $.tags 0
"\"user\""
# Find index of value
JSON.ARRINDEX user:1001 $.tags '"vip"'
(integer) 1
# Trim array (keep indices 0-1)
JSON.ARRTRIM user:1001 $.tags 0 16. String Operations
String Commands
JSON.SET user:1001 $ '{"bio":"Hello"}'
# Append to string
JSON.STRAPPEND user:1001 $.bio '" World"'
(integer) 11 # New length
# Get string length
JSON.STRLEN user:1001 $.bio
(integer) 11 # "Hello World"7. Object Operations
Object Commands
JSON.SET user:1001 $ '{"name":"John","email":"john@example.com"}'
# Get all keys
JSON.OBJKEYS user:1001 $
1) "name"
2) "email"
# Get key count
JSON.OBJLEN user:1001 $
(integer) 2
# Merge objects (Redis 7.2+)
JSON.MERGE user:1001 $ '{"phone":"123-456-7890","verified":true}'
OK8. Type Checking
JSON.TYPE
JSON.SET data $ '{"str":"hello","num":42,"arr":[1,2,3],"obj":{"a":1},"flag":true,"empty":null}'
JSON.TYPE data $.str # "string"
JSON.TYPE data $.num # "integer"
JSON.TYPE data $.arr # "array"
JSON.TYPE data $.obj # "object"
JSON.TYPE data $.flag # "boolean"
JSON.TYPE data $.empty # "null"9. CLI vs Redimo
JSON in CLI is still JSON - escaped quotes, no syntax highlighting, hard to navigate nested structures.
CLI Output
127.0.0.1:6379> JSON.GET user:1001
"{\"name\":\"John\",\"profile\":{\"settings\":{\"theme\":\"dark\",\"notifications\":{\"email\":true,\"push\":false}}}}"
# Even with INDENT it's hard to navigateRedimo View
- • JSON rendered as expandable tree
- • Click to edit any nested value
- • Syntax highlighting
- • Type indicators (string, number, boolean)
- • Copy path to any field
10. Common Patterns
User Profile
# Store complete profile
JSON.SET user:1001 $ '{
"name": "John Doe",
"email": "john@example.com",
"preferences": {
"theme": "dark",
"language": "en",
"notifications": {
"email": true,
"push": false
}
},
"roles": ["user", "admin"]
}'
# Update single preference
JSON.SET user:1001 $.preferences.theme '"light"'
# Add a role
JSON.ARRAPPEND user:1001 $.roles '"moderator"'
# Get just preferences
JSON.GET user:1001 $.preferencesShopping Cart
# Initialize cart
JSON.SET cart:user:1001 $ '{
"items": [],
"total": 0
}'
# Add item
JSON.ARRAPPEND cart:user:1001 $.items '{"productId":"P001","qty":2,"price":29.99}'
JSON.NUMINCRBY cart:user:1001 $.total 59.98
# Update quantity
JSON.SET cart:user:1001 '$.items[0].qty' '3'
# Remove item by index
JSON.ARRPOP cart:user:1001 $.items 0Quick Reference
| Command | Purpose |
|---|---|
JSON.SET | Store/update value at path |
JSON.GET | Retrieve value(s) at path(s) |
JSON.DEL | Delete value at path |
JSON.NUMINCRBY | Increment number |
JSON.ARRAPPEND | Append to array |
JSON.ARRPOP | Pop from array |
JSON.TYPE | Get JSON type at path |
Work with JSON
RedisJSON turns Redis into a document store. Navigate your JSON visually with Redimo.
Download Redimo - It's Free