Home > other >  How to GET Redis JSON data
How to GET Redis JSON data

Time:10-02

I am, unfortunately, using an out dated Redis so it does not support the new JSON commands, so I am doing a JSON.Stringify(object) and doing a redis SET which stores the key like so:

"{\"id\":\"123\",\"person\":\"Joe\""}"

What Redis command can I do to get this key based off the id?

I was trying to play around with

redis-cli get id:123

but this was not working. Any ideas?

CodePudding user response:

The Redis SET command associates a key with a value. What that key and value are is up to you; Redis doesn't interpret them in any way, or extract them from a single object, you pass them as two arguments.

For instance:

  • SET hello world associates the key "hello" with the value "world"
  • SET "address-123" "123 Blah St, Nashville, TN 12345" associates the key "address-123" with the value "123 Blah St, Nashville, TN 12345"
  • SET "6b091891-58eb-4563-9f9b-8ac893cf4725" "{\"id\":\"6b091891-58eb-4563-9f9b-8ac893cf4725\",\"address\":\"123 Blah St, Nashville, TN 12345\""}" associates the key "6b091891-58eb-4563-9f9b-8ac893cf4725" with the value "{\"id\":\"6b091891-58eb-4563-9f9b-8ac893cf4725\",\"address\":\"123 Blah St, Nashville, TN 12345\""}"

The GET command then retrieves the value based on the key. So, given the above SET commands:

  • GET hello will return "world"
  • GET "address-123" will return "123 Blah St, Nashville, TN 12345"
  • GET "6b091891-58eb-4563-9f9b-8ac893cf4725" will return "{\"id\":\"6b091891-58eb-4563-9f9b-8ac893cf4725\",\"address\":\"123 Blah St, Nashville, TN 12345\""}"

Using the entire object as the key doesn't make much sense, because in order to look it up again, you need exactly the same object - in which case, there's nothing to look up.

  • Related