Skip to content

Commit

Permalink
Merge branch 'master' into Cleaning2
Browse files Browse the repository at this point in the history
  • Loading branch information
shacharPash committed Sep 27, 2023
2 parents 70b5624 + cfeb817 commit ec38563
Show file tree
Hide file tree
Showing 78 changed files with 939 additions and 466 deletions.
89 changes: 75 additions & 14 deletions Examples/AdvancedJsonExamples.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# Advanced JSON

Redis JSON array filtering examples

## Contents
1. [Business Value Statement](#value)
2. [Data Set](#dataset)
3. [Data Loading](#dataload)
4. [Array Filtering Examples](#arrayfiltering)
1. [All Properties of Array](#allprops)
2. [All Properties of a Field](#allfield)
3. [Relational - Equality](#equality)
4. [Relational - Less Than](#lessthan)
5. [Relational - Greater Than or Equal](#greaterthan)
6. [Logical AND](#logicaland)
7. [Logical OR](#logicalor)
8. [Regex - Contains Exact](#regex_exact)
9. [Regex - Contains, Case Insensitive](#regex_contains)
10. [Regex - Begins With](#regex_begins)

1. [Business Value Statement](#value)
2. [Data Set](#dataset)
3. [Data Loading](#dataload)
4. [Array Filtering Examples](#arrayfiltering)
1. [All Properties of Array](#allprops)
2. [All Properties of a Field](#allfield)
3. [Relational - Equality](#equality)
4. [Relational - Less Than](#lessthan)
5. [Relational - Greater Than or Equal](#greaterthan)
6. [Logical AND](#logicaland)
7. [Logical OR](#logicalor)
8. [Regex - Contains Exact](#regex_exact)
9. [Regex - Contains, Case Insensitive](#regex_contains)
10. [Regex - Begins With](#regex_begins)

## Business Value Statement <a name="value"></a>

The ability to query within a JSON object unlocks further value to the underlying data. Redis supports JSONPath array filtering natively.

## Data Set <a name="dataset"></a>

```JSON
{
"city": "Boston",
Expand Down Expand Up @@ -48,7 +54,9 @@ The ability to query within a JSON object unlocks further value to the underlyin
]
}
```

## Data Loading <a name="dataload"></a>

```c#
JsonCommands json = db.JSON();
json.Set("warehouse:1", "$", new {
Expand Down Expand Up @@ -79,21 +87,29 @@ json.Set("warehouse:1", "$", new {
}
});
```

## Array Filtering Examples <a name="arrayfiltering"></a>

### Syntax

[JSON.GET](https://redis.io/commands/json.get/)

### All Properties of Array <a name="allprops"></a>

Fetch all properties of an array.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[*]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand Down Expand Up @@ -131,16 +147,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### All Properties of a Field <a name="allfield"></a>

Fetch all values of a field within an array.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[*].price",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
34.95,
Expand All @@ -150,16 +171,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Relational - Equality <a name="equality"></a>

Fetch all items within an array where a text field matches a given value.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description==\"Turtle Check Men Navy Blue Shirt\")]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand All @@ -176,16 +202,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Relational - Less Than <a name="lessthan"></a>

Fetch all items within an array where a numeric field is less than a given value.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.price<100)]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand All @@ -211,16 +242,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Relational - Greater Than or Equal <a name="greaterthan"></a>

Fetch all items within an array where a numeric field is greater than or equal to a given value.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.id>=20000)]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand Down Expand Up @@ -248,16 +284,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Logical AND <a name="logicaland"></a>

Fetch all items within an array that meet two relational operations.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.gender==\"Men\"&&@.price>20)]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand All @@ -274,16 +315,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Logical OR <a name="logicalor"></a>

Fetch all items within an array that meet at least one relational operation. In this case, return only the ids of those items.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.price<100||@.gender==\"Women\")].id",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
15970,
Expand All @@ -293,16 +339,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Regex - Contains Exact <a name="regex_exact"></a>

Fetch all items within an array that match a given regex pattern.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description =~ \"Blue\")]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand All @@ -328,16 +379,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Regex - Contains, Case Insensitive <a name="regex_contains"></a>

Fetch all items within an array where a field contains a term, case insensitive.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description =~ \"(?i)watch\")]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand All @@ -356,16 +412,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
```

### Regex - Begins With <a name="regex_begins"></a>

Fetch all items within an array where a field begins with a given expression.

#### Command

```c#
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description =~ \"^T\")]",
indent: "\t",
newLine: "\n"
));
```

#### Result

```json
[
{
Expand Down
Loading

0 comments on commit ec38563

Please sign in to comment.