column name "note" – json type
data in one cell of the column is written in the following way :-
[
{"text":"bbb","userID":"U001","time":16704,"showInReport":true},
{"text":"bb","userID":"U001","time":167047,"showInReport":true}
]
interval note column containing data
how to find value of key text which contains ‘bb’
which postgersql query can be used to find results
I used below query which works but if someone gives value as userid or text then it shows wrong result
I’m using Postgres 10.20 version
select distinct(workflowid)
from cyto_records r
join cyto_record_results rr on (r.recordid = rr.recordid)
where rr.interval_note::text LIKE '%aaa%';
3
Answers
Try using JSON_EXTRACT
ref: https://dev.mysql.com/doc/refman/8.0/en/json.html#json-paths
For Postgres versions 11 and older, you need to iterate over the array elements:
If you are using Postgres 12 or newer, you can use a JSON path expression:
This assumes that
interval_note
is defined with the recommended typejsonb
. If it’s not, you need to cast it:interval_note::jsonb
Unrelated to your question, but: the
distinct
operator is not a function.Enclosing a column after that in parentheses is useless won’t change a thing
distinct (a),b
is the same asdistinct a,(b)
ordistinct a,b
In fact it’s an "option" to the SELECT keyword:
SELECT ALL
vs.SELECT DISTINCT
– similar toUNION ALL
andUNION DISTINCT
.I would suggest to first flatten using lateral join and then perform ‘plain’ select.
Postgres version before 12:
Postgres version 12+ using
jsonb_path_query
: