Home > Back-end >  psql how to select if any properties in array of object if a certain value
psql how to select if any properties in array of object if a certain value

Time:09-23

I want to select every row that has args.args_json.actions ==> any id to 79

{
  "args": {
    "args_json": {
      "actions": [
        {
          "id": 79
        },
        {
          "id": 47

        },
        {
          "id": 82
        }
      ]
    }
  }
}

I tried args #> '{args_json,actions,0}' ->> 'id' = '79' and it works but only to check index 0.

I would like the same but returns true if any of id is 79, not only the index 0.

CodePudding user response:

args #> '{args_json,actions}' @> '[{"id":79}]' ;

CodePudding user response:

There is no need to dereference, you can simply use the containment operator @> with a bigger JSON:

WHERE args @> '{ "args_json": { "actions": [ { "id":79 } ] } }'
  • Related