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
key
: (Required) The key under which the JSON data is stored in DiceDB.path
: (Optional) A JSONPath expression to specify the part of the JSON document to retrieve. If not provided, the entire JSON document is returned.
Return Value
The command returns the JSON data stored at the specified key and path. The data is returned as a JSON string. If the specified key or path does not exist, the command returns nil
.
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.
Error Handling
The JSON.GET
command can raise the following errors:
(error) ERR wrong number of arguments for 'JSON.GET' command
: This error occurs if the command is called with an incorrect number of arguments.(error) ERR key does not exist
: This error occurs if the specified key does not exist in the DiceDB database.(error) ERR invalid path
: This error occurs if the provided JSONPath expression is invalid or does not match any part of the JSON document.
Example Usage
Example 1: Retrieve Entire JSON Document
Example 2: Retrieve Specific Field from JSON Document
Example 3: Retrieve Nested Field from JSON Document
Example 4: Handling Non-Existent Key
Example 5: Handling Invalid 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 DiceDBJSON.