From f595bbd335971bd6b8acdb93adc8b2f145aad8ee Mon Sep 17 00:00:00 2001 From: Neville Mehta Date: Mon, 30 Sep 2024 22:56:04 -0700 Subject: [PATCH] Audit and make documentation for command JSON.CLEAR consistent --- docs/src/content/docs/commands/JSON.CLEAR.md | 39 +++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/docs/src/content/docs/commands/JSON.CLEAR.md b/docs/src/content/docs/commands/JSON.CLEAR.md index 253bdbfb45..c76edaf26d 100644 --- a/docs/src/content/docs/commands/JSON.CLEAR.md +++ b/docs/src/content/docs/commands/JSON.CLEAR.md @@ -5,14 +5,25 @@ description: Documentation for the DiceDB command JSON.CLEAR The `JSON.CLEAR` command is part of the DiceDBJSON module, which allows you to manipulate JSON data stored in DiceDB. This command is used to clear the value at a specified path in a JSON document, effectively setting it to an empty state. This can be particularly useful when you want to reset a part of your JSON document without removing the key itself. +## Syntax + +``` +JSON.CLEAR key [path] +``` + ## Parameters -- `key`: (String) The key under which the JSON document is stored. -- `path`: (String) The path within the JSON document that you want to clear. The path should be specified in JSONPath format. If the path is omitted, the root path (`$`) is assumed. +| Parameter | Description | Type | Required | +|-----------|---------------------------------------------------------------------------|---------|----------| +| `key` | (String) The key under which the JSON document is stored. | String | Yes | +| `path` | (String) The path within the JSON document that you want to clear. The path should be specified in JSONPath format. If the path is omitted, the root path (`$`) is assumed. | String | No | -## Return Value +## Return values -- `Integer`: The number of paths that were cleared. +| Condition | Return Value | +|------------------------------------------------|---------------------------------------------------| +| Command is successful | `Integer` (The number of paths that were cleared) | +| Syntax or specified constraints are invalid | error | ## Behaviour @@ -20,24 +31,26 @@ When the `JSON.CLEAR` command is executed, it traverses the JSON document stored - For objects, it removes all key-value pairs. - For arrays, it removes all elements. -- For strings, it sets the value to an empty string. +- For strings, the value remaines unchanged. - For numbers, it sets the value to `0`. -- For booleans, it sets the value to `false`. +- For booleans, the value remaines unchanged. If the specified path does not exist, the command does nothing and returns `0`. -## Error Handling +## Errors The `JSON.CLEAR` command can raise the following errors: - `(error) ERR wrong number of arguments for 'json.clear' command`: This error is raised if the command is called with an incorrect number of arguments. -- `(error) ERR key does not exist`: This error is raised if the specified key does not exist in the DiceDB database. -- `(error) ERR path is not a valid JSONPath`: This error is raised if the specified path is not a valid JSONPath expression. -- `(error) ERR path does not exist`: This error is raised if the specified path does not exist within the JSON document. +- `(error) ERR could not perform this operation on a key that doesn't exist`: This error is raised if the specified key does not exist in the DiceDB database. +- `(error) ERR invalid JSONPath`: This error is raised if the specified path is not a valid JSONPath expression. +- `(error) ERR Existing key has wrong Dice type`: This error is raised if the key exists but the value is not of the expected JSON type and encoding. + +Note: If the specified path does not exist within the JSON document, the command will not raise an error but will simply not modify anything. ## Example Usage -### Example 1: Clearing a JSON Object +### Clearing a JSON Object Suppose you have a JSON document stored under the key `user:1001`: @@ -68,7 +81,7 @@ After executing this command, the JSON document would be: } ``` -### Example 2: Clearing an Array +### Clearing an Array Suppose you have a JSON document stored under the key `user:1002`: @@ -94,7 +107,7 @@ After executing this command, the JSON document would be: } ``` -### Example 3: Clearing the Root Path +### Clearing the Root Path Suppose you have a JSON document stored under the key `user:1003`: