I am trying to select a segment if:
settings
dict is not null- Has key
user_parameters
in it - And
user_parameters
(dict) has keyuser_id
in it
Code below:
user_id = '100952'
select([self.db.segments]).where(
and_(
self.db.segments.c.settings.isnot(None),
cast(self.db.segments.c.settings, JSON)["user_parameters"].as_string().contains(f"'{user_id}'"),
)
)
Converting JSON to a string and searching for user_id
in that string is not reliable, as it does not ensure that the captured string will be a key; it may also match a value. What is the correct way to check if user_id
exists as a key in the settings['user_parameters']
dictionary?
2
Answers
You could use
jsonb_path_exists
.I haven’t tested the example, but it should look like this:
More info in the sqlalchemy docs and json functions in postgres.
This should work: