I have to work with a text column, but its content is json format. An example:
{"company":"company name","entityName":"entity name","asOfDate":1604725200000}
Right now, I am using LIKE to get the results I want:
select distinct a.company_name from company_table a
where a.json_field LIKE '%' || ?1 || '%'
I would like to be able to query the json directly:
select distinct a.company_name from company_table a
where a.json_field ->> company = ?1
A third option would be getting all the rows, then use code to parse through them, I'm fairly certain that the growth of table is low and won't reach like 10k/year.
While the LIKE method works, I don't know if it's 100% correct.
Any suggestions?
Just cast
it:
where (a.json_field::jsonb) ->> company = ?1
This will error if your string is not valid JSON.
I would recommend fixing your schema, and convert that column to JSON once and for all, so you don't need to worry about this anymore. You can do this in a single statement:
alter table company_table
alter column json_field type jsonb
using json_field::jsonb;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.