Search for a field content with json

Hello,
I have structure with a filed named named “languages” where we can have N languages with the level. For example:
languages:
1[description: “english”, level : “9”]
2[description: “italian”, level: “5”]

Is possible do a search looking in the “languages” field with the next predicate "where language is “french” with level greater than 4 or language is “italian” with level greater than “3”?

Thanks a lot in advance!

You can use a JSONPath filter, (see docs on ?() filter and examples) e.g.,

With this:

127.0.0.1:6379> JSON.SET k1 $ '{"languages":[{"description":"english","level":9},{"description":"italian","level":5}]}'
OK
127.0.0.1:6379> JSON.SET k2 $ '{"languages":[{"description":"french","level":9},{"description":"italian","level":2}]}'
OK
127.0.0.1:6379> JSON.SET k3 $ '{"languages":[{"description":"english","level":9},{"description":"italian","level":2}]}'
OK

You can filter like this:

127.0.0.1:6379> JSON.GET k1 '$.languages[?((@.description=="french" && @.level>4)||(@.description=="italian" && @.level>3))]'
"[{\"description\":\"italian\",\"level\":5}]"
127.0.0.1:6379> JSON.GET k2 '$.languages[?((@.description=="french" && @.level>4)||(@.description=="italian" && @.level>3))]'
"[{\"description\":\"french\",\"level\":9}]"
127.0.0.1:6379> JSON.GET k3 '$.languages[?((@.description=="french" && @.level>4)||(@.description=="italian" && @.level>3))]'
"[]"
1 Like

Thanks @oshadmi, I will try it :+1: