0

How would I check to see if a value exists with name Dan in column sess from a table called Table1 with 3 columns called id, sess, and timeD.

Sess type is JSON

Table1

id    sess                                         timeD
1     {"cookie":{expires:null}, "name": "Joey"}    12:30
2     {"cookie":{expires:null}, "name": "Dan"}      1:00
3     {"cookie":{expires:null}, "name": "Bob"}      1:20
6
  • @bca I'm checking to see if name has value Dan in the sess column. I'm not checking if the column is null Commented Sep 7, 2021 at 18:41
  • stackoverflow.com/questions/19422233/… can perhaps help? Commented Sep 7, 2021 at 18:44
  • @jarlh I'm reading it but like how would you determine the name is Bob? Commented Sep 7, 2021 at 18:54
  • What is the column type of "sess"? Commented Sep 7, 2021 at 19:11
  • Generally you should provide the relevant schema (columns and types), indexes, postgres version, etc. Commented Sep 7, 2021 at 19:13

2 Answers 2

0

This depends on the column type of sess. If it's a string you can use a string match, e.g.

where sess like '%"name": "Dan"%'

If it's json you can query for values inside the json object using JSON functions and operators, e.g.

where sess->>'name' = 'Bob'

Or if it's a string type but contains JSON you can parse the string to JSON then query on the JSON like:

where sess::json->>'name' = 'Bob'
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the contains operator: @>

where sess @> '{"name": "Dan"}'

this assumes that sess is defined as a jsonb column (which it should be). If it's not, you need to cast it: sess::jsonb @> ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.