1

I have a jsonb column, which has a structure like this:

{
    .....other fields,
    "a" : {
        "b" : {
            "c" : "some value",
            ....other fields
        }
        .....other fields
    },
    ...other fields
}

I can have query like this, properties being column name in table MyTable:

SELECT * from
  MyTable t
WHERE t.properties @> '{"a":{"b":{"c": "some value"}}}';

But it is possible that fields before "c" are different,ie we can have:

{
        .....other fields,
        "m" : {
            "n" : {
                "c" : "some value",
                ....other fields
            }
            .....other fields
        },
        ...other fields
    }

How to modify my query for such a scenario?

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Mandroid
  • 6,200
  • 12
  • 64
  • 134

1 Answers1

2

If the nesting is always at the same level you can use a JSON/Path expression:

select *
from the_table
where properties @@ '$.*.*.c == "some value"'