0

I have JSON data(see the ex below) which I'm storing in Redis list using 'rpush' with a key as 'person'. Ex data:

[
  { "name": "john", "age": 30, "role": "developer" },
  { "name": "smith", "age": 45, "role": "manager" },
  { "name": "ram", "age": 35, "role": "tester" },
]

Now when I get this data using lrange person 0 -1, it gives me results as '[object object]'.

So, to actually get them with property names I'm storing them by stringifying them and parsing them back to objects to use the object properties.

But the issue with converting to a string is that I'm not able to sort them using any property, say name, age or role.

My question is, how do I store this JSON in Redis list and sort them using any of the properties.

Thanks.

Gowthamss
  • 191
  • 1
  • 2
  • 13
  • Is there a reason you don't store them as JSON? There is a RedisJSON extension available for redis, which is also included in Redis Stack. – goose_lake Nov 10 '22 at 10:24
  • Yeah, I tried to use RedisJSON but couldn't enable it on my server. – Gowthamss Nov 10 '22 at 11:10
  • I would recommend trying more, achieving what you are trying to do with string-type entries is a big pain. Search for "redis modules", or "enable redis json" with whatever details of your environment. – goose_lake Nov 10 '22 at 11:16
  • Yeah, I have been trying for that. BTW, do we have sorting available on RedisJSON? I have also seen that RedisJSON is available for Redis Enterprise Cloud subscription only. Is that right? I don't have one. – Gowthamss Nov 10 '22 at 11:39
  • I enabled RedisJSON on my server now. But I couldn't find any command to sort the data stored using it. – Gowthamss Nov 11 '22 at 08:32

1 Answers1

0

Very recently I posted an answer for a very similar question.

The easiest approach is to use Redis Search module (which makese the approach portable to many clients / languages):

  • Store each needed object as separate key, following a prefixed key pattern (keys named prefix:something), and standard schema (all user keys are JSON, and all contain the field you want to sort).
  • Make a search index with FT.CREATE, with ON JSON parameter to search JSON-type keys, and likely PREFIX parameter to search just the needed keys, as well as x AS y parameters for all needed search fields, where x is field name, and y is type of field (TEXT, TAG, NUMERIC, etc. -- see documentation), optionally adding SORTABLE to the fields if they need to be sorted.
  • Use FT.SEARCH command with any combination of "@field=value" search parameters, and optionally SORTBY.

Otherwise, it's possible to just get all keys that follow a pattern using KEYS command, and use manual language-specific sorting code. That is of course more involved, depends on language and available libraries, and is therefore less portable.

goose_lake
  • 847
  • 2
  • 15
  • Hi @goose_lake, I want to store all the json objects in an array and apply sort on that. I think we have 'JSON.ARRAPPEND' command to store all the json objects in an array. I would also like to know if there is any Redis command to sort them instead of custom sort on that array. – Gowthamss Nov 16 '22 at 05:30
  • If you really require them to be in a single array (which is less performant), you should get the entire array you stored them in with `JSON.GET $.path.to.your.array`, and then use a whatever sort is available in the language/library you use. Most languages have predicate-based sort, see [here](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) for JS example. – goose_lake Nov 23 '22 at 17:01
  • Yeah that is what I'm also thinking but fetching data using JSON.GET is slower compared to storing them stringified using normal Redis get command. I have observed this behavior by writing a small benchmark. So, that is also a blockage. – Gowthamss Nov 24 '22 at 05:12