Let’s use this to keep track of questions or various tricks related to RedisJSON commands.
I would like to know why the value of the JSON.SET command must be quoted with single quotes specifically.
For example, the following command throws an invalid argument(s) error:
JSON.SET colors . ["blue", "red", "green"]
its predefined syntax
JSON.SET <key> <path> <json>
In your example your are trying to pass array value which is not accepted as per syntax, it expects JSON value.
The reason why you have to set up quotes Simple or double is that, the command takes a JSON String, and buy setting the quotes it allow the command to be parsed properly.
So you can do:
> JSON.SET colors . "[\"blue\",\"green\"]" // here in your example you forgot to escape the "
> JSON.SET colors . '["blue","green"]'
Note that you need quotes because you are storing string and the command and json parser need to understand it, but for example this command works too (and it does not have any quotes)
> JSON.SET nums . [0,1,2,3.4,10] // valid json with any space!
But I would prefer to use this syntax:
> JSON.SET nums . "[0,1,2,3.4,10]"
You can now do:
> JSON.ARRLEN colors .
(integer) 2
> JSON.ARRLEN nums .
(integer) 5
But I think your documents may be a little more complex in real life for example:
> JSON.SET doc:1 . '{"name":"my beatiful doc", "colors":["blue","green"], "nums":[0,1,2,3.4,10], "counter":1}'
And now:
> JSON.GET doc:1 name
"\"my beatiful doc\""
> JSON.ARRLEN doc:1 colors
(integer) 2
> JSON.ARRAPPEND doc:1 nums 22
(integer) 6
> JSON.ARRAPPEND doc:1 colors '"yellow"'
(integer) 3
So it is better to use the simple quote to make send your JSON content.
Note:
- your client library will take care of most of the things for you
- the RedisJSON community is improving all this in release 2.0
Thank you so much @tgrall for that explanation. It makes this much clearer.
Using json on Redis Clud server:
Redis Clud offers the following:
-30 MB of storage included, no credit card required
-Central business functions
- [NEW] RediSearch, RedisGraph, RedisJSON, RedisBloom, and RedisTimeSeries modules are now available for free in the AWS / Mumbai region
-Access to developer tools, including the RedisInsight GUI