JSON.GET
The JSON.GET
command allows you to store, update, and retrieve JSON values in DiceDB. The JSON.GET
command retrieves JSON data stored at a specified key. This command is useful for fetching JSON objects, arrays, or values from the DiceDB database.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key against which the JSON data is stored in DiceDB | String | Yes |
path | A JSONPath expression to specify the part of the JSON document to retrieve. If not provided, the entire JSON document is returned. Default value is $ (root) | String | No |
Return Values
Condition | Return Value |
---|---|
The specified key does not exists | nil |
The specified key exists and path argument is not specified | String : The entire JSON data for the key |
The specified key exists and the specified path exists in the JSON data | String : The data for the key at the specified path |
The specified key exists and specified path does not exists in the JSON data | error |
Syntax or specified constraints are invalid | error |
Behaviour
When the JSON.GET
command is executed:
- DiceDB checks if the specified key exists.
- If the key exists, DiceDB retrieves the JSON data stored at that key.
- If a path is provided, DiceDB extracts the specified part of the JSON document using the JSONPath expression.
- The retrieved JSON data is returned as a JSON string.
Errors
Incorrect number of arguments
- Error Message:
(error) ERR wrong number of arguments for 'json.get' command
- Error Message:
Invalid JSONPath expression
- Error Message:
(error) ERR invalid JSONPath
- Error Message:
Non-Existent JSONPath in the JSON data stored against a key
- Error Message:
(error) ERR Path '$.<path>' does not exist
- Error Message:
Example Usage
Retrieve Entire JSON Document
Retrieve Specific Field from JSON Document
Retrieve Nested Field from JSON Document
Handling Non-Existent Key
Handling Non-Existent Path
Notes
- JSONPath expressions allow you to navigate and retrieve specific parts of a JSON document. Ensure that your JSONPath expressions are correctly formatted to avoid errors.
By understanding the JSON.GET
command, you can efficiently retrieve JSON data stored in your DiceDB database, enabling you to build powerful and flexible applications that leverage the capabilities of DiceDB.