Command: JSON.SET
Overview
The JSON.SET
command allows clients to store structured JSON objects under a specific key in a distributed, in-memory data store. If the key already exists, the stored JSON value is overwritten. This is particularly useful for schema-less data use cases like storing user profiles, configurations, and nested records.
Command Name
JSON.SET
Description
Stores a valid JSON object at the specified key. If the key already exists, its previous value is replaced. The value must be a valid JSON object (e.g., not a number, string, or array as the top-level structure). This supports flexible, nested data models and is typically used for structured configurations, user metadata, and document storage.
Use Cases
-
Store user profile data:
{"name": "Alice", "age": 27}
-
Save application settings:
{"theme": "dark", "notifications": {"email": true, "sms": false}}
-
Manage nested business objects or configuration trees.
Syntax
JSON.SET <key> <json_value>
-
<key>
: String identifier under which the JSON object is stored. -
<json_value>
: A stringified JSON object (must be valid JSON). The top-level structure must be an object ({}
).
Permissions
-
Write Access Required: The user/client must have write access to the targeted key’s shard.
-
Internally WAL (Write-Ahead Log) records the command for durability and persistence.
Examples
Basic JSON Set
localhost:9219> JSON.SET myObj '{"name":"John","age":30,"hobbies":["reading","hiking"]}'
Ok
Retrieve It
localhost:9219> JSON.GET myObj
Ok {
"age": 30,
"hobbies": [
"reading",
"hiking"
],
"name": "John"
}
Update with Nested Object
localhost:9219> JSON.SET myObj '{"name":"John Doe","age":30,"isActive":true,"hobbies":["reading","hiking"],"address":{"street":"123 Main St","city":"New York"}}'
Ok
localhost:9219> JSON.GET myObj
Ok {
"address": {
"city": "New York",
"street": "123 Main St"
},
"age": 30,
"hobbies": [
"reading",
"hiking"
],
"isActive": true,
"name": "John Doe"
}
Behavior on Error
Condition | Error Message |
---|---|
Missing key or value | InvalidKeyError: Key must be provided |
Incorrect number of arguments | InvalidArgsError: invalid number of arguments, Syntax: LIST.FILTER <key> <path> |
Invalid JSON value | InvalidValueError: provided value not a valid json object |