Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#833: update JSON.FORGET doc and make it consistent with SET command doc #912

Merged
merged 4 commits into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 50 additions & 65 deletions docs/src/content/docs/commands/JSON.FORGET.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,92 @@ title: JSON.FORGET
description: Documentation for the DiceDB command JSON.FORGET
---

The `JSON.FORGET` command is part of the DiceDBJSON module, which allows you to manipulate JSON data stored in DiceDB. This command is used to delete a specified path from a JSON document stored at a given key. If the path leads to an array element, the element is removed, and the array is reindexed.
The `JSON.FORGET` command in DiceDB is used to delete a specified path from a JSON document stored at a given key. If the path leads to an array element, the element is removed, and the array is reindexed. This is useful for modifying and updating portions of a JSON document.

## Syntax

```
JSON.FORGET key path
```

## Parameters

- `key`: (String) The key under which the JSON document is stored.
- `path`: (String) The JSONPath expression specifying the part of the JSON document to be deleted. The path must be a valid JSONPath expression.
| Parameter | Description | Type | Required |
|-----------|------------------------------------------------------------------------------|---------|----------|
| `key` | The name of the key under which the JSON document is stored. | String | Yes |
| `path` | The JSONPath expression specifying the part of the JSON document to delete. | String | Yes |

## Return Value
## Return values

- `Integer`: The number of paths that were deleted. If the specified path does not exist, the command returns `0`.
| Condition | Return Value |
|--------------------------------------|-----------------------------------------|
| Command successfully deletes a path | `The number of paths deleted (Integer)` |
| Path does not exist | `0` |
| Invalid key or path | error |

## Behaviour

When the `JSON.FORGET` command is executed, the following actions occur:
- The command locates the JSON document stored at the specified key.
- It evaluates the provided JSONPath expression to identify the part of the document to be deleted.
- If the specified path exists, the targeted part is deleted.
- If the path leads to an array element, the element is removed, and the array is reindexed.
- The command returns the number of paths that were successfully deleted.

1. The command locates the JSON document stored at the specified key.
2. It evaluates the provided JSONPath expression to identify the part of the document to be deleted.
3. If the path is valid and exists, the specified part of the JSON document is removed.
4. If the path leads to an array element, the element is removed, and the array is reindexed.
5. The command returns the number of paths that were successfully deleted.
## Errors

## Error Handling
1. `Wrong number of arguments`:

The `JSON.FORGET` command can raise the following errors:
- Error Message: `(error) ERR wrong number of arguments for 'JSON.FORGET' command`
- Occurs when the command is called with an incorrect number of arguments.

- `(error) ERR wrong number of arguments for 'JSON.FORGET' 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 malformed.
- `(error) ERR path does not exist`: This error occurs if the specified path does not exist within the JSON document.
2. `Key does not exist`:

## Example Usage
- Error Message: `(error) ERR key does not exist`
- Occurs if the specified key is not present in the database.

### Example 1: Deleting a Field from a JSON Document
3. `Invalid JSONPath expression`:

Suppose we have a JSON document stored at the key `user:1001`:
- Error Message: `(error) ERR invalid path`
- Occurs when the provided JSONPath expression is invalid or malformed.

```json
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
```

To delete the `age` field from this document, you would use the following command:
4. `Path does not exist`:

```sh
JSON.FORGET user:1001 $.age
```

`Expected Output:`
- Error Message: `(error) ERR path does not exist`
- Occurs if the specified path does not exist within the JSON document.

```sh
(integer) 1
```
## Example Usage

### Example 2: Deleting an Element from a JSON Array
### Basic Usage

Suppose we have a JSON document stored at the key `user:1002`:
Deleting the `age` field from a JSON document stored at key `user:1001`:

```json
{
"name": "Jane Doe",
"hobbies": ["reading", "swimming", "hiking"]
}
```bash
127.0.0.1:7379> JSON.FORGET user:1001 $.age
(integer) 1
```

To delete the second element (`"swimming"`) from the `hobbies` array, you would use the following command:
### Deleting an Element from a JSON Array

```sh
JSON.FORGET user:1002 $.hobbies[1]
```

`Expected Output:`
Deleting the second element (`"swimming"`) from the `hobbies` array in the JSON document stored at key `user:1002`:

```sh
```bash
127.0.0.1:7379> JSON.FORGET user:1002 $.hobbies[1]
(integer) 1
```

The updated JSON document would be:

#### The updated document:
```json
{
"name": "Jane Doe",
"hobbies": ["reading", "hiking"]
}
```

### Example 3: Deleting a Non-Existent Path

Suppose we have the same JSON document as in Example 1. If you attempt to delete a non-existent path:

```sh
JSON.FORGET user:1001 $.nonexistent
```
### Deleting a Non-Existent Path

`Expected Output:`
Attempting to delete a non-existent path in the document at key `user:1001`:

```sh
```bash
127.0.0.1:7379> JSON.FORGET user:1001 $.nonexistent
(integer) 0
```