JSON
JSON support for Redis
The JSON capability of Redis Stack provides JavaScript Object Notation (JSON) support for Redis. It lets you store, update, and retrieve JSON values in a Redis database, similar to any other Redis data type. Redis JSON also works seamlessly with Search and Query to let you index and query JSON documents.
Primary features
- Full support for the JSON standard
- A JSONPath syntax for selecting/updating elements inside documents (see JSONPath syntax)
- Documents stored as binary data in a tree structure, allowing fast access to sub-elements
- Typed atomic operations for all JSON value types
Use Redis JSON
The first JSON command to try is JSON.SET
, which sets a Redis key with a JSON value. JSON.SET
accepts all JSON value types. This example creates a JSON string:
> JSON.SET bike $ '"Hyperion"'
OK
> JSON.GET bike $
"[\"Hyperion\"]"
> JSON.TYPE bike $
1) "string"
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.json().set("bike", "$", '"Hyperion"')
print(res1) # >>> True
res2 = r.json().get("bike", "$")
print(res2) # >>> ['"Hyperion"']
res3 = r.json().type("bike", "$")
print(res3) # >>> ['string']
res4 = r.json().strlen("bike", "$")
print(res4) # >>> [10]
res5 = r.json().strappend("bike", '" (Enduro bikes)"')
print(res5) # >>> 27
res6 = r.json().get("bike", "$")
print(res6) # >>> ['"Hyperion"" (Enduro bikes)"']
res7 = r.json().set("crashes", "$", 0)
print(res7) # >>> True
res8 = r.json().numincrby("crashes", "$", 1)
print(res8) # >>> [1]
res9 = r.json().numincrby("crashes", "$", 1.5)
print(res9) # >>> [2.5]
res10 = r.json().numincrby("crashes", "$", -0.75)
print(res10) # >>> [1.75]
res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None])
print(res11) # >>> True
res12 = r.json().get("newbike", "$")
print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]']
res13 = r.json().get("newbike", "$[1].crashes")
print(res13) # >>> ['0']
res14 = r.json().delete("newbike", "$.[-1]")
print(res14) # >>> [1]
res15 = r.json().get("newbike", "$")
print(res15) # >>> [['Deimos', {'crashes': 0}]]
res16 = r.json().set("riders", "$", [])
print(res16) # >>> True
res17 = r.json().arrappend("riders", "$", "Norem")
print(res17) # >>> [1]
res18 = r.json().get("riders", "$")
print(res18) # >>> [['Norem']]
res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla")
print(res19) # >>> [4]
res20 = r.json().get("riders", "$")
print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']]
res21 = r.json().arrtrim("riders", "$", 1, 1)
print(res21) # >>> [1]
res22 = r.json().get("riders", "$")
print(res22) # >>> [['Prickett']]
res23 = r.json().arrpop("riders", "$")
print(res23) # >>> ['"Prickett"']
res24 = r.json().arrpop("riders", "$")
print(res24) # >>> [None]
res25 = r.json().set(
"bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972}
)
print(res25) # >>> True
res26 = r.json().objlen("bike:1", "$")
print(res26) # >>> [3]
res27 = r.json().objkeys("bike:1", "$")
print(res27) # >>> [['model', 'brand', 'price']]
inventory_json = {
"inventory": {
"mountain_bikes": [
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike "
"impressed our testers and is the best all-around trail bike we've "
"ever tested. The Shimano gear system effectively does away with an "
"external cassette, so is super low maintenance in terms of wear "
"and tear. All in all it's an impressive package for the price, "
"making it very competitive.",
"price": 2072,
"specs": {"material": "aluminium", "weight": 7.9},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
],
"commuter_bikes": [
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
],
}
}
res1 = r.json().set("bikes:inventory", "$", inventory_json)
print(res1) # >>> True
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model")
print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model')
print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model")
print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res6 = r.json().get("bikes:inventory", "$..model")
print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']]
res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model")
print(res7) # >>> [['Phoebe', 'Quaoar']]
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> ['Weywot', 'Mimas']
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)
res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]
res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res20) # >>> [3, 3]
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
Note how the commands include the dollar sign character $
. This is the path to the value in the JSON document (in this case it just means the root).
Here are a few more string operations. JSON.STRLEN
tells you the length of the string, and you can append another string to it with JSON.STRAPPEND
.
> JSON.STRLEN bike $
1) (integer) 8
> JSON.STRAPPEND bike $ '" (Enduro bikes)"'
1) (integer) 23
> JSON.GET bike $
"[\"Hyperion (Enduro bikes)\"]"
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.json().set("bike", "$", '"Hyperion"')
print(res1) # >>> True
res2 = r.json().get("bike", "$")
print(res2) # >>> ['"Hyperion"']
res3 = r.json().type("bike", "$")
print(res3) # >>> ['string']
res4 = r.json().strlen("bike", "$")
print(res4) # >>> [10]
res5 = r.json().strappend("bike", '" (Enduro bikes)"')
print(res5) # >>> 27
res6 = r.json().get("bike", "$")
print(res6) # >>> ['"Hyperion"" (Enduro bikes)"']
res7 = r.json().set("crashes", "$", 0)
print(res7) # >>> True
res8 = r.json().numincrby("crashes", "$", 1)
print(res8) # >>> [1]
res9 = r.json().numincrby("crashes", "$", 1.5)
print(res9) # >>> [2.5]
res10 = r.json().numincrby("crashes", "$", -0.75)
print(res10) # >>> [1.75]
res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None])
print(res11) # >>> True
res12 = r.json().get("newbike", "$")
print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]']
res13 = r.json().get("newbike", "$[1].crashes")
print(res13) # >>> ['0']
res14 = r.json().delete("newbike", "$.[-1]")
print(res14) # >>> [1]
res15 = r.json().get("newbike", "$")
print(res15) # >>> [['Deimos', {'crashes': 0}]]
res16 = r.json().set("riders", "$", [])
print(res16) # >>> True
res17 = r.json().arrappend("riders", "$", "Norem")
print(res17) # >>> [1]
res18 = r.json().get("riders", "$")
print(res18) # >>> [['Norem']]
res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla")
print(res19) # >>> [4]
res20 = r.json().get("riders", "$")
print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']]
res21 = r.json().arrtrim("riders", "$", 1, 1)
print(res21) # >>> [1]
res22 = r.json().get("riders", "$")
print(res22) # >>> [['Prickett']]
res23 = r.json().arrpop("riders", "$")
print(res23) # >>> ['"Prickett"']
res24 = r.json().arrpop("riders", "$")
print(res24) # >>> [None]
res25 = r.json().set(
"bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972}
)
print(res25) # >>> True
res26 = r.json().objlen("bike:1", "$")
print(res26) # >>> [3]
res27 = r.json().objkeys("bike:1", "$")
print(res27) # >>> [['model', 'brand', 'price']]
inventory_json = {
"inventory": {
"mountain_bikes": [
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike "
"impressed our testers and is the best all-around trail bike we've "
"ever tested. The Shimano gear system effectively does away with an "
"external cassette, so is super low maintenance in terms of wear "
"and tear. All in all it's an impressive package for the price, "
"making it very competitive.",
"price": 2072,
"specs": {"material": "aluminium", "weight": 7.9},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
],
"commuter_bikes": [
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
],
}
}
res1 = r.json().set("bikes:inventory", "$", inventory_json)
print(res1) # >>> True
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model")
print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model')
print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model")
print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res6 = r.json().get("bikes:inventory", "$..model")
print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']]
res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model")
print(res7) # >>> [['Phoebe', 'Quaoar']]
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> ['Weywot', 'Mimas']
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)
res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]
res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res20) # >>> [3, 3]
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
Numbers can be incremented and multiplied:
> JSON.SET crashes $ 0
OK
> JSON.NUMINCRBY crashes $ 1
"[1]"
> JSON.NUMINCRBY crashes $ 1.5
"[2.5]"
> JSON.NUMINCRBY crashes $ -0.75
"[1.75]"
> JSON.NUMMULTBY crashes $ 24
"[42]"
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.json().set("bike", "$", '"Hyperion"')
print(res1) # >>> True
res2 = r.json().get("bike", "$")
print(res2) # >>> ['"Hyperion"']
res3 = r.json().type("bike", "$")
print(res3) # >>> ['string']
res4 = r.json().strlen("bike", "$")
print(res4) # >>> [10]
res5 = r.json().strappend("bike", '" (Enduro bikes)"')
print(res5) # >>> 27
res6 = r.json().get("bike", "$")
print(res6) # >>> ['"Hyperion"" (Enduro bikes)"']
res7 = r.json().set("crashes", "$", 0)
print(res7) # >>> True
res8 = r.json().numincrby("crashes", "$", 1)
print(res8) # >>> [1]
res9 = r.json().numincrby("crashes", "$", 1.5)
print(res9) # >>> [2.5]
res10 = r.json().numincrby("crashes", "$", -0.75)
print(res10) # >>> [1.75]
res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None])
print(res11) # >>> True
res12 = r.json().get("newbike", "$")
print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]']
res13 = r.json().get("newbike", "$[1].crashes")
print(res13) # >>> ['0']
res14 = r.json().delete("newbike", "$.[-1]")
print(res14) # >>> [1]
res15 = r.json().get("newbike", "$")
print(res15) # >>> [['Deimos', {'crashes': 0}]]
res16 = r.json().set("riders", "$", [])
print(res16) # >>> True
res17 = r.json().arrappend("riders", "$", "Norem")
print(res17) # >>> [1]
res18 = r.json().get("riders", "$")
print(res18) # >>> [['Norem']]
res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla")
print(res19) # >>> [4]
res20 = r.json().get("riders", "$")
print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']]
res21 = r.json().arrtrim("riders", "$", 1, 1)
print(res21) # >>> [1]
res22 = r.json().get("riders", "$")
print(res22) # >>> [['Prickett']]
res23 = r.json().arrpop("riders", "$")
print(res23) # >>> ['"Prickett"']
res24 = r.json().arrpop("riders", "$")
print(res24) # >>> [None]
res25 = r.json().set(
"bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972}
)
print(res25) # >>> True
res26 = r.json().objlen("bike:1", "$")
print(res26) # >>> [3]
res27 = r.json().objkeys("bike:1", "$")
print(res27) # >>> [['model', 'brand', 'price']]
inventory_json = {
"inventory": {
"mountain_bikes": [
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike "
"impressed our testers and is the best all-around trail bike we've "
"ever tested. The Shimano gear system effectively does away with an "
"external cassette, so is super low maintenance in terms of wear "
"and tear. All in all it's an impressive package for the price, "
"making it very competitive.",
"price": 2072,
"specs": {"material": "aluminium", "weight": 7.9},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
],
"commuter_bikes": [
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
],
}
}
res1 = r.json().set("bikes:inventory", "$", inventory_json)
print(res1) # >>> True
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model")
print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model')
print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model")
print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res6 = r.json().get("bikes:inventory", "$..model")
print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']]
res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model")
print(res7) # >>> [['Phoebe', 'Quaoar']]
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> ['Weywot', 'Mimas']
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)
res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]
res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res20) # >>> [3, 3]
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
Here's a more interesting example that includes JSON arrays and objects:
> JSON.SET newbike $ '["Deimos", {"crashes": 0}, null]'
OK
> JSON.GET newbike $
"[[\"Deimos\",{\"crashes\":0},null]]"
> JSON.GET newbike $[1].crashes
"[0]"
> JSON.DEL newbike $[-1]
(integer) 1
> JSON.GET newbike $
"[[\"Deimos\",{\"crashes\":0}]]"
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.json().set("bike", "$", '"Hyperion"')
print(res1) # >>> True
res2 = r.json().get("bike", "$")
print(res2) # >>> ['"Hyperion"']
res3 = r.json().type("bike", "$")
print(res3) # >>> ['string']
res4 = r.json().strlen("bike", "$")
print(res4) # >>> [10]
res5 = r.json().strappend("bike", '" (Enduro bikes)"')
print(res5) # >>> 27
res6 = r.json().get("bike", "$")
print(res6) # >>> ['"Hyperion"" (Enduro bikes)"']
res7 = r.json().set("crashes", "$", 0)
print(res7) # >>> True
res8 = r.json().numincrby("crashes", "$", 1)
print(res8) # >>> [1]
res9 = r.json().numincrby("crashes", "$", 1.5)
print(res9) # >>> [2.5]
res10 = r.json().numincrby("crashes", "$", -0.75)
print(res10) # >>> [1.75]
res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None])
print(res11) # >>> True
res12 = r.json().get("newbike", "$")
print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]']
res13 = r.json().get("newbike", "$[1].crashes")
print(res13) # >>> ['0']
res14 = r.json().delete("newbike", "$.[-1]")
print(res14) # >>> [1]
res15 = r.json().get("newbike", "$")
print(res15) # >>> [['Deimos', {'crashes': 0}]]
res16 = r.json().set("riders", "$", [])
print(res16) # >>> True
res17 = r.json().arrappend("riders", "$", "Norem")
print(res17) # >>> [1]
res18 = r.json().get("riders", "$")
print(res18) # >>> [['Norem']]
res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla")
print(res19) # >>> [4]
res20 = r.json().get("riders", "$")
print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']]
res21 = r.json().arrtrim("riders", "$", 1, 1)
print(res21) # >>> [1]
res22 = r.json().get("riders", "$")
print(res22) # >>> [['Prickett']]
res23 = r.json().arrpop("riders", "$")
print(res23) # >>> ['"Prickett"']
res24 = r.json().arrpop("riders", "$")
print(res24) # >>> [None]
res25 = r.json().set(
"bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972}
)
print(res25) # >>> True
res26 = r.json().objlen("bike:1", "$")
print(res26) # >>> [3]
res27 = r.json().objkeys("bike:1", "$")
print(res27) # >>> [['model', 'brand', 'price']]
inventory_json = {
"inventory": {
"mountain_bikes": [
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike "
"impressed our testers and is the best all-around trail bike we've "
"ever tested. The Shimano gear system effectively does away with an "
"external cassette, so is super low maintenance in terms of wear "
"and tear. All in all it's an impressive package for the price, "
"making it very competitive.",
"price": 2072,
"specs": {"material": "aluminium", "weight": 7.9},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
],
"commuter_bikes": [
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
],
}
}
res1 = r.json().set("bikes:inventory", "$", inventory_json)
print(res1) # >>> True
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model")
print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model')
print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model")
print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res6 = r.json().get("bikes:inventory", "$..model")
print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']]
res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model")
print(res7) # >>> [['Phoebe', 'Quaoar']]
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> ['Weywot', 'Mimas']
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)
res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]
res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res20) # >>> [3, 3]
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
The JSON.DEL
command deletes any JSON value you specify with the path
parameter.
You can manipulate arrays with a dedicated subset of JSON commands:
> JSON.SET riders $ []
OK
> JSON.ARRAPPEND riders $ '"Norem"'
1) (integer) 1
> JSON.GET riders $
"[[\"Norem\"]]"
> JSON.ARRINSERT riders $ 1 '"Prickett"' '"Royce"' '"Castilla"'
1) (integer) 4
> JSON.GET riders $
"[[\"Norem\",\"Prickett\",\"Royce\",\"Castilla\"]]"
> JSON.ARRTRIM riders $ 1 1
1) (integer) 1
> JSON.GET riders $
"[[\"Prickett\"]]"
> JSON.ARRPOP riders $
1) "\"Prickett\""
> JSON.ARRPOP riders $
1) (nil)
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.json().set("bike", "$", '"Hyperion"')
print(res1) # >>> True
res2 = r.json().get("bike", "$")
print(res2) # >>> ['"Hyperion"']
res3 = r.json().type("bike", "$")
print(res3) # >>> ['string']
res4 = r.json().strlen("bike", "$")
print(res4) # >>> [10]
res5 = r.json().strappend("bike", '" (Enduro bikes)"')
print(res5) # >>> 27
res6 = r.json().get("bike", "$")
print(res6) # >>> ['"Hyperion"" (Enduro bikes)"']
res7 = r.json().set("crashes", "$", 0)
print(res7) # >>> True
res8 = r.json().numincrby("crashes", "$", 1)
print(res8) # >>> [1]
res9 = r.json().numincrby("crashes", "$", 1.5)
print(res9) # >>> [2.5]
res10 = r.json().numincrby("crashes", "$", -0.75)
print(res10) # >>> [1.75]
res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None])
print(res11) # >>> True
res12 = r.json().get("newbike", "$")
print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]']
res13 = r.json().get("newbike", "$[1].crashes")
print(res13) # >>> ['0']
res14 = r.json().delete("newbike", "$.[-1]")
print(res14) # >>> [1]
res15 = r.json().get("newbike", "$")
print(res15) # >>> [['Deimos', {'crashes': 0}]]
res16 = r.json().set("riders", "$", [])
print(res16) # >>> True
res17 = r.json().arrappend("riders", "$", "Norem")
print(res17) # >>> [1]
res18 = r.json().get("riders", "$")
print(res18) # >>> [['Norem']]
res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla")
print(res19) # >>> [4]
res20 = r.json().get("riders", "$")
print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']]
res21 = r.json().arrtrim("riders", "$", 1, 1)
print(res21) # >>> [1]
res22 = r.json().get("riders", "$")
print(res22) # >>> [['Prickett']]
res23 = r.json().arrpop("riders", "$")
print(res23) # >>> ['"Prickett"']
res24 = r.json().arrpop("riders", "$")
print(res24) # >>> [None]
res25 = r.json().set(
"bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972}
)
print(res25) # >>> True
res26 = r.json().objlen("bike:1", "$")
print(res26) # >>> [3]
res27 = r.json().objkeys("bike:1", "$")
print(res27) # >>> [['model', 'brand', 'price']]
inventory_json = {
"inventory": {
"mountain_bikes": [
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike "
"impressed our testers and is the best all-around trail bike we've "
"ever tested. The Shimano gear system effectively does away with an "
"external cassette, so is super low maintenance in terms of wear "
"and tear. All in all it's an impressive package for the price, "
"making it very competitive.",
"price": 2072,
"specs": {"material": "aluminium", "weight": 7.9},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
],
"commuter_bikes": [
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
],
}
}
res1 = r.json().set("bikes:inventory", "$", inventory_json)
print(res1) # >>> True
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model")
print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model')
print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model")
print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res6 = r.json().get("bikes:inventory", "$..model")
print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']]
res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model")
print(res7) # >>> [['Phoebe', 'Quaoar']]
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> ['Weywot', 'Mimas']
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)
res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]
res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res20) # >>> [3, 3]
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
JSON objects also have their own commands:
> JSON.SET bike:1 $ '{"model": "Deimos", "brand": "Ergonom", "price": 4972}'
OK
> JSON.OBJLEN bike:1 $
1) (integer) 3
> JSON.OBJKEYS bike:1 $
1) 1) "model"
2) "brand"
3) "price"
"""
Code samples for JSON doc pages: https://redis.io/docs/latest/develop/data-types/json/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.json().set("bike", "$", '"Hyperion"')
print(res1) # >>> True
res2 = r.json().get("bike", "$")
print(res2) # >>> ['"Hyperion"']
res3 = r.json().type("bike", "$")
print(res3) # >>> ['string']
res4 = r.json().strlen("bike", "$")
print(res4) # >>> [10]
res5 = r.json().strappend("bike", '" (Enduro bikes)"')
print(res5) # >>> 27
res6 = r.json().get("bike", "$")
print(res6) # >>> ['"Hyperion"" (Enduro bikes)"']
res7 = r.json().set("crashes", "$", 0)
print(res7) # >>> True
res8 = r.json().numincrby("crashes", "$", 1)
print(res8) # >>> [1]
res9 = r.json().numincrby("crashes", "$", 1.5)
print(res9) # >>> [2.5]
res10 = r.json().numincrby("crashes", "$", -0.75)
print(res10) # >>> [1.75]
res11 = r.json().set("newbike", "$", ["Deimos", {"crashes": 0}, None])
print(res11) # >>> True
res12 = r.json().get("newbike", "$")
print(res12) # >>> ['["Deimos", { "crashes": 0 }, null]']
res13 = r.json().get("newbike", "$[1].crashes")
print(res13) # >>> ['0']
res14 = r.json().delete("newbike", "$.[-1]")
print(res14) # >>> [1]
res15 = r.json().get("newbike", "$")
print(res15) # >>> [['Deimos', {'crashes': 0}]]
res16 = r.json().set("riders", "$", [])
print(res16) # >>> True
res17 = r.json().arrappend("riders", "$", "Norem")
print(res17) # >>> [1]
res18 = r.json().get("riders", "$")
print(res18) # >>> [['Norem']]
res19 = r.json().arrinsert("riders", "$", 1, "Prickett", "Royce", "Castilla")
print(res19) # >>> [4]
res20 = r.json().get("riders", "$")
print(res20) # >>> [['Norem', 'Prickett', 'Royce', 'Castilla']]
res21 = r.json().arrtrim("riders", "$", 1, 1)
print(res21) # >>> [1]
res22 = r.json().get("riders", "$")
print(res22) # >>> [['Prickett']]
res23 = r.json().arrpop("riders", "$")
print(res23) # >>> ['"Prickett"']
res24 = r.json().arrpop("riders", "$")
print(res24) # >>> [None]
res25 = r.json().set(
"bike:1", "$", {"model": "Deimos", "brand": "Ergonom", "price": 4972}
)
print(res25) # >>> True
res26 = r.json().objlen("bike:1", "$")
print(res26) # >>> [3]
res27 = r.json().objkeys("bike:1", "$")
print(res27) # >>> [['model', 'brand', 'price']]
inventory_json = {
"inventory": {
"mountain_bikes": [
{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic "
"daily driver or one bike quiver. The Shimano Claris 8-speed groupset "
"gives plenty of gear range to tackle hills and there\u2019s room for "
"mudguards and a rack too. This is the bike for the rider who wants "
"trail manners with low fuss ownership.",
"price": 1920,
"specs": {"material": "carbon", "weight": 13.1},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike "
"impressed our testers and is the best all-around trail bike we've "
"ever tested. The Shimano gear system effectively does away with an "
"external cassette, so is super low maintenance in terms of wear "
"and tear. All in all it's an impressive package for the price, "
"making it very competitive.",
"price": 2072,
"specs": {"material": "aluminium", "weight": 7.9},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older "
"a durable and uberlight mountain bike for their first experience "
"on tracks and easy cruising through forests and fields. A set of "
"powerful Shimano hydraulic disc brakes provide ample stopping "
"ability. If you're after a budget option, this is one of the best "
"bikes you could get.",
"price": 3264,
"specs": {"material": "alloy", "weight": 13.8},
},
],
"commuter_bikes": [
{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just "
"wants a bike to get about on With a slick-shifting Claris gears "
"from Shimano\u2019s, this is a bike which doesn\u2019t break the "
"bank and delivers craved performance. It\u2019s for the rider "
"who wants both efficiency and capability.",
"price": 1475,
"specs": {"material": "aluminium", "weight": 16.6},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high "
"scores in last years Bike of the year report. The carefully "
"crafted 50-34 tooth chainset and 11-32 tooth cassette give an "
"easy-on-the-legs bottom gear for climbing, and the high-quality "
"Vittoria Zaffiro tires give balance and grip.It includes "
"a low-step frame , our memory foam seat, bump-resistant shocks and "
"conveniently placed thumb throttle. Put it all together and you "
"get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {"material": "alloy", "weight": 11.6},
},
],
}
}
res1 = r.json().set("bikes:inventory", "$", inventory_json)
print(res1) # >>> True
res2 = r.json().get("bikes:inventory", "$.inventory.*")
print(res2)
# >>> [[{'id': 'bike:1', 'model': 'Phoebe',
# >>> 'description': 'This is a mid-travel trail slayer...
res3 = r.json().get("bikes:inventory", "$.inventory.mountain_bikes[*].model")
print(res3) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res4 = r.json().get("bikes:inventory", '$.inventory["mountain_bikes"][*].model')
print(res4) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res5 = r.json().get("bikes:inventory", "$..mountain_bikes[*].model")
print(res5) # >>> [['Phoebe', 'Quaoar', 'Weywot']]
res6 = r.json().get("bikes:inventory", "$..model")
print(res6) # >>> [['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']]
res7 = r.json().get("bikes:inventory", "$..mountain_bikes[0:2].model")
print(res7) # >>> [['Phoebe', 'Quaoar']]
res8 = r.json().get(
"bikes:inventory",
"$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]",
)
print(res8)
# >>> [{'id': 'bike:2', 'model': 'Quaoar',
# 'description': "Redesigned for the 2020 model year...
# names of bikes made from an alloy
res9 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material == 'alloy')].model"
)
print(res9) # >>> ['Weywot', 'Mimas']
res10 = r.json().get(
"bikes:inventory", "$..[?(@.specs.material =~ '(?i)al')].model"
)
print(res10) # >>> ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
res11 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[0].regex_pat", "(?i)al"
)
res12 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[1].regex_pat", "(?i)al"
)
res13 = r.json().set(
"bikes:inventory", "$.inventory.mountain_bikes[2].regex_pat", "(?i)al"
)
res14 = r.json().get(
"bikes:inventory",
"$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model",
)
print(res14) # >>> ['Quaoar', 'Weywot']
res15 = r.json().get("bikes:inventory", "$..price")
print(res15) # >>> [1920, 2072, 3264, 1475, 3941]
res16 = r.json().numincrby("bikes:inventory", "$..price", -100)
print(res16) # >>> [1820, 1972, 3164, 1375, 3841]
res17 = r.json().numincrby("bikes:inventory", "$..price", 100)
print(res17) # >>> [1920, 2072, 3264, 1475, 3941]
res18 = r.json().set(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].price", 1500
)
res19 = r.json().get("bikes:inventory", "$..price")
print(res19) # >>> [1500, 2072, 3264, 1500, 3941]
res20 = r.json().arrappend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
)
print(res20) # >>> [3, 3]
res21 = r.json().get("bikes:inventory", "$..[*].colors")
print(
res21
) # >>> [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
Format CLI output
The CLI has a raw output mode that lets you add formatting to the output from
JSON.GET
to make
it more readable. To use this, run redis-cli
with the --raw
option
and include formatting keywords such as INDENT
, NEWLINE
, and SPACE
with JSON.GET
:
$ redis-cli --raw
> JSON.GET obj INDENT "\t" NEWLINE "\n" SPACE " " $
[
{
"name": "Leonard Cohen",
"lastSeen": 1478476800,
"loggedOut": true
}
]
Enable Redis JSON
Redis JSON is implemented as a module that extends the basic Redis server. Redis Stack and Redis Enterprise include this module by default but you can also load it dynamically if it isn't already available. The sections below the different ways you can access Redis JSON.
Run with Docker
To run RedisJSON with Docker, use the redis-stack-server
Docker image:
$ docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
For more information about running Redis Stack in a Docker container, see Run Redis Stack on Docker.
Download binaries
To download and run the RedisJSON module that provides the JSON data structure from a precompiled binary:
-
Download a precompiled version from the Redis download center.
-
Load the module it in Redis
$ redis-server --loadmodule /path/to/module/src/rejson.so
Build from source
To build RedisJSON from the source code:
-
Clone the repository (make sure you include the
--recursive
option to properly clone submodules):$ git clone --recursive https://github.com/RedisJSON/RedisJSON.git $ cd RedisJSON
-
Install dependencies:
$ ./sbin/setup
-
Build:
$ make build
Load the module to Redis
Requirements:
Generally, it is best to run the latest Redis version.
If your OS has a Redis 6.x package or later, you can install it using the OS package manager.
Otherwise, you can invoke
$ ./deps/readies/bin/getredis
To load the RedisJSON module, use one of the following methods:
Makefile recipe
Run Redis with RedisJSON:
$ make run
Configuration file
Or you can have Redis load the module during startup by adding the following to your redis.conf
file:
loadmodule /path/to/module/target/release/librejson.so
On Mac OS, if this module was built as a dynamic library, run:
loadmodule /path/to/module/target/release/librejson.dylib
In the above lines replace /path/to/module/
with the actual path to the module.
Command-line option
You can also have Redis load the module using the following command-line argument syntax:
$ redis-server --loadmodule /path/to/module/librejson.so
In the above lines replace /path/to/module/
with the actual path to the module's library.
MODULE LOAD
command
You can also use the MODULE LOAD
command to load RedisJSON. Note that MODULE LOAD
is a dangerous command and may be blocked/deprecated in the future due to security considerations.
After the module has been loaded successfully, the Redis log should have lines similar to:
...
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> version: 20009 git sha: d8d4b19 branch: HEAD
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> Exported RedisJSON_V1 API
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> Enabled diskless replication
9:M 11 Aug 2022 16:24:06.701 * <ReJSON> Created new data type 'ReJSON-RL'
9:M 11 Aug 2022 16:24:06.701 * Module 'ReJSON' loaded from /opt/redis-stack/lib/rejson.so
...
Limitation
A JSON value passed to a command can have a depth of up to 128. If you pass to a command a JSON value that contains an object or an array with a nesting level of more than 128, the command returns an error.
Further information
Read the other pages in this section to learn more about Redis JSON