[Avg. reading time: 7 minutes]
Redis JSON
Redis Stack extends the core features of Redis OSS and provides a complete developer experience for debugging and more.
- RedisJSON
- RedisGraph
- RedisTimeseries
- RedisSearch
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.
JSON.SET friends:character:rachel $ '{"name": "Rachel Green", "occupation": "Fashion Executive", "relationship_status": "Single", "friends": ["Ross Geller", "Monica Geller", "Joey Tribbiani", "Chandler Bing", "Phoebe Buffay"] }'
Dollar sign ($) represents the Root node
JSON.GET friends:character:rachel
Retrieve Specific fields
JSON.GET friends:character:rachel $.name $.occupation
Adds education at the end
JSON.SET friends:character:rachel $.education '
{"high_school": "Lincoln High School", "college": "Not specified" }'
JSON.GET friends:character:rachel
Adding Array of values
JSON.SET friends:character:rachel $.employment_history '[ { "company": "Central Perk", "position": "Waitress", "years": "1994-1995" }, { "company": "Bloomingdale\'s", "position": "Assistant Buyer", "years": "1996-1999" }, { "company": "Ralph Lauren", "position": "Executive", "years": "1999-2004" } ]'
Get Employment History
json.get friends:character:rachel employment_history
JSON.GET friends:character:rachel $.employment_history[*].company
Get specific one
json.get friends:character:rachel employment_history[1]
Scan All Keys
SCAN 0 MATCH friends:character:*
Add more data
JSON.SET friends:character:ross $ '{
"name": "Ross Geller",
"occupation": "Paleontologist",
"relationship_status": "Divorced",
"friends": ["Rachel Green", "Monica Geller", "Joey Tribbiani", "Chandler Bing", "Phoebe Buffay"],
"children": [
{
"name": "Ben Geller",
"mother": "Carol Willick"
},
{
"name": "Emma Geller-Green",
"mother": "Rachel Green"
}
],
"education": {
"college": "Columbia University",
"degree": "Ph.D. in Paleontology"
}}'
JSON.SET friends:character:monica $ '{
"name": "Monica Geller",
"occupation": "Chef",
"relationship_status": "Married",
"friends": ["Ross Geller", "Rachel Green", "Joey Tribbiani", "Chandler Bing", "Phoebe Buffay"],
"spouse": "Chandler Bing",
"education": {
"culinary_school": "Not specified"
},
"employment_history": [
{
"company": "Alessandro\'s",
"position": "Head Chef",
"years": "Not specified"
},
{
"company": "Javu",
"position": "Chef",
"years": "Not specified"
}
]}'
JSON.GET friends:character:ross $.name $.occupation
JSON.SET friends:character:chandler $ '{
"name": "Chandler Bing",
"occupation": "Statistical analysis and data reconfiguration",
"relationship_status": "Married",
"friends": ["Ross Geller", "Monica Geller", "Joey Tribbiani", "Rachel Green", "Phoebe Buffay"],
"spouse": "Monica Geller",
"education": {
"college": "Not specified"
}}'
JSON.SET friends:character:phoebe $ '{
"name": "Phoebe Buffay",
"occupation": "Masseuse and Musician",
"relationship_status": "Married",
"friends": ["Ross Geller", "Monica Geller", "Joey Tribbiani", "Chandler Bing", "Rachel Green"],
"spouse": "Mike Hannigan",
"education": {
"high_school": "Not completed"
}}'
JSON.SET friends:character:joey $ '{
"name": "Joey Tribbiani",
"occupation": "Actor",
"relationship_status": "Single",
"friends": ["Ross Geller", "Monica Geller", "Chandler Bing", "Rachel Green", "Phoebe Buffay"],
"education": {
"drama_school": "Not specified"
},
"employment_history": [
{
"show": "Days of Our Lives",
"role": "Dr. Drake Ramoray",
"years": "Various"
}
]}'
Delete specific node
JSON.DEL friends:character:monica $.occupation