From a928fe9cfb8957b652fd6571439926f06be7870c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D1=80=D0=B8=D0=B3=D0=BE=D1=80=D0=B8=D0=B9=20=D0=A1?= =?UTF-8?q?=D0=B0=D1=84=D1=80=D0=BE=D0=BD=D0=BE=D0=B2?= Date: Sat, 22 Mar 2025 18:46:26 +0000 Subject: [PATCH] Update README.md --- README.md | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/README.md b/README.md index 0884c29..2f19308 100644 --- a/README.md +++ b/README.md @@ -121,3 +121,112 @@ JSON.STRLEN JSON.TOGGLE JSON.TYPE ``` + +## Usage Example + +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" +``` +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)\"]" + +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]" +``` + +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}]]" + +``` +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) + +``` + +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" + + +``` + +## Format Cli Output + +``` +$ valkey-cli --raw +> JSON.GET obj INDENT "\t" NEWLINE "\n" SPACE " " $ +[ + { + "name": "Leonard Cohen", + "lastSeen": 1478476800, + "loggedOut": true + } +] +```