How to dynamically set a value based on another key-value

I found a solution based on this former post. By rewriting the example object to an array containing the key-value for comparison.

{
  "options": [
    {
      "color": "red",
      "emotion": "sad",
      "condition2": "bad"
    },
    {
      "color": "green",
      "emotion": "happy",
      "condition2": "good"
    },
    {
      "color": "yellow",
      "emotion": "meh",
      "condition2": "neutral"
    }
  ],
  "condition1": "good"
}

Then Recursively copy the outer condition into each of the array objects and finally make a value setter to copy the matching object using jmespath:
myCondition.string = &{options[?condition2 == condition1]}

Still it would be interesting to know if this can be done directly without the recursive copying.

1 Like