I am trying to search a table in a LIKE %str% fashion but on fields inside json values over multiple columns.
I have a table which has three jsonb columns change,previous and specific_changes. As you might imagine the content is JSON but the structure of that json is not know ahead of time, therefor i can't use the -> or ->> in query like so:
select * from change_log where change -> 'field' = '"something"'
create table change_log
(
id serial not null
constraint pk_change_log
primary key,
change jsonb not null,
previous jsonb,
changed_at timestamp with time zone default timezone('utc'::text, now()),
specific_changes jsonb
);
INSERT INTO public.change_log (id, change, previous, changed_at, specific_changes) VALUES (1, '{"val": 2, "test": "test", "nested": {"nval": 1}}', 'null', '2020-11-12 16:53:28.827896', '{"val2": "Value2"}');
INSERT INTO public.change_log (id, change, previous, changed_at, specific_changes) VALUES (2, '{"val": "testNewChange", "test": "testChange", "nested": {"key": 1}}', '{"val": "2", "test": "testChange", "nested": {"nval": 1}}', '2020-11-15 12:18:35.021843', '{"new": "testValue"}');
INSERT INTO public.change_log (id, change, previous, changed_at, specific_changes) VALUES (3, '{"val": "newewChange", "test": "changeNew", "nested": {"val": 3}}', '{"val": "testNewChange", "test": "testValue", "nested": {"key": 1}}', '2020-11-15 12:19:40.832843', '{"new": "testChange", "nested": {"val": 1}}');
My question is:
How would a query look like that given a string will return all rows from the change_log table whose any of the 3 mentioned jsonb columns contain any fields that has a value
like %string%.how would you make the query case insensitive
Examples:
INPUT OUTPUT(ids)
"2" (1,2)
"Change" (2,3)
"Chan" (2,3)
"Value" (1,2,3)
EDIT1: I am using postgres version 9.6
EDIT2: Fixed inserted changes to reflect desired behavior
id=1is omitted forChangewhile it contains"test": "testChange"key? Do you want to check the nested objects as well? If yes, whyid=3is not included for2value while it contains"nested": {"val": 2}?