2

I'm trying to access the object with property optionId = 'a386ead3-08ca-486e-aeb1-23add87292e7' to set its weight. my object is like following:

weight": {
            "options": [
              {
                "optionId": "a386ead3-08ca-486e-aeb1-23add87292e7",
                "weight": 10
              },
              {
                "optionId": "a386ead3-08ca-486e-aeb1-23add87292e7",
                "weight": 20
              }
            ],
            "value": 100
          }

and i'm using the following function to get its path but with no luck:

local GetFieldOptionWeightPath = function (optionId)
    return "$.weight.options[\"optionId\"==\""..optionId.."\"]";
end
Nifim
  • 4,758
  • 2
  • 12
  • 31
Mohammed Ehab
  • 207
  • 1
  • 5
  • 14

1 Answers1

0

You need to compare each optionId like you would programmatically. To do that, you can use a filter script expression:

$.weight.options[?(@.optionId=="a386ead3-08ca-486e-aeb1-23add87292e7")]

Here, $() is the filter and @ will point to each of the elements that's getting filtered.

Note that since it's a filter, it may potentially yield multiple results. In fact, in your example case it will yield both entries as they have the same optionId.

In the end your Lua function generating the path can look like:

local GetFieldOptionWeightPath = function (optionId)
    return ("$.weight.options[?(@.optionId==%q)]"):format(optionId)
end

This answer assumes JSONPath support which was implemented in RedisJSON v2 (late 2021).

Aki
  • 2,818
  • 1
  • 15
  • 23