Redis JSON array filtering examples
The ability to query within a JSON object unlocks further value to the underlying data. Redis supports JSONPath array filtering natively.
{
"city": "Boston",
"location": "42.361145, -71.057083",
"inventory": [
{
"id": 15970,
"gender": "Men",
"season":["Fall", "Winter"],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
},
{
"id": 59263,
"gender": "Women",
"season": ["Fall", "Winter", "Spring", "Summer"],
"description": "Titan Women Silver Watch",
"price": 129.99
},
{
"id": 46885,
"gender": "Boys",
"season": ["Fall"],
"description": "Ben 10 Boys Navy Blue Slippers",
"price": 45.99
}
]
}
JsonCommands json = db.JSON();
json.Set("warehouse:1", "$", new {
city = "Boston",
location = "42.361145, -71.057083",
inventory = new[] {
new {
id = 15970,
gender = "Men",
season = new[] {"Fall", "Winter"},
description = "Turtle Check Men Navy Blue Shirt",
price = 34.95
},
new {
id = 59263,
gender = "Women",
season = new[] {"Fall", "Winter", "Spring", "Summer"},
description = "Titan Women Silver Watch",
price = 129.99
},
new {
id = 46885,
gender = "Boys",
season = new[] {"Fall"},
description = "Ben 10 Boys Navy Blue Slippers",
price = 45.99
}
}
});
Fetch all properties of an array.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[*]",
indent: "\t",
newLine: "\n"
));
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
},
{
"id":59263,
"gender":"Women",
"season":[
"Fall",
"Winter",
"Spring",
"Summer"
],
"description":"Titan Women Silver Watch",
"price":129.99
},
{
"id":46885,
"gender":"Boys",
"season":[
"Fall"
],
"description":"Ben 10 Boys Navy Blue Slippers",
"price":45.99
}
]
Fetch all values of a field within an array.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[*].price",
indent: "\t",
newLine: "\n"
));
[
34.95,
129.99,
45.99
]
Fetch all items within an array where a text field matches a given value.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description==\"Turtle Check Men Navy Blue Shirt\")]",
indent: "\t",
newLine: "\n"
));
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
}
]
Fetch all items within an array where a numeric field is less than a given value.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.price<100)]",
indent: "\t",
newLine: "\n"
));
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
},
{
"id":46885,
"gender":"Boys",
"season":[
"Fall"
],
"description":"Ben 10 Boys Navy Blue Slippers",
"price":45.99
}
]
Fetch all items within an array where a numeric field is greater than or equal to a given value.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.id>=20000)]",
indent: "\t",
newLine: "\n"
));
[
{
"id":59263,
"gender":"Women",
"season":[
"Fall",
"Winter",
"Spring",
"Summer"
],
"description":"Titan Women Silver Watch",
"price":129.99
},
{
"id":46885,
"gender":"Boys",
"season":[
"Fall"
],
"description":"Ben 10 Boys Navy Blue Slippers",
"price":45.99
}
]
Fetch all items within an array that meet two relational operations.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.gender==\"Men\"&&@.price>20)]",
indent: "\t",
newLine: "\n"
));
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
}
]
Fetch all items within an array that meet at least one relational operation. In this case, return only the ids of those items.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.price<100||@.gender==\"Women\")].id",
indent: "\t",
newLine: "\n"
));
[
15970,
59263,
46885
]
Fetch all items within an array that match a given regex pattern.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description =~ \"Blue\")]",
indent: "\t",
newLine: "\n"
));
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
},
{
"id":46885,
"gender":"Boys",
"season":[
"Fall"
],
"description":"Ben 10 Boys Navy Blue Slippers",
"price":45.99
}
]
Fetch all items within an array where a field contains a term, case insensitive.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description =~ \"(?i)watch\")]",
indent: "\t",
newLine: "\n"
));
[
{
"id":59263,
"gender":"Women",
"season":[
"Fall",
"Winter",
"Spring",
"Summer"
],
"description":"Titan Women Silver Watch",
"price":129.99
}
]
Fetch all items within an array where a field begins with a given expression.
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.description =~ \"^T\")]",
indent: "\t",
newLine: "\n"
));
[
{
"id":59263,
"gender":"Women",
"season":[
"Fall",
"Winter",
"Spring",
"Summer"
],
"description":"Titan Women Silver Watch",
"price":129.99
}
]
*** Lab 4 - Regex - Begins With ***
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
},
{
"id":59263,
"gender":"Women",
"season":[
"Fall",
"Winter",
"Spring",
"Summer"
],
"description":"Titan Women Silver Watch",
"price":129.99
}
]