Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

To add the value use the JSON array append opperator (||) UPDATE jsontesting SET jsondata = jsondata || ‘[“newString”]’::jsonb WHERE id = 7; Removing the value looks like this UPDATE jsontesting SET jsondata = jsondata – ‘newString’ WHERE id = 7; Concatenating to a nested field looks like this UPDATE jsontesting SET jsondata = jsonb_set( … Read more

Operator does not exist: json = json

In short – use JSONB instead of JSON or cast JSON to JSONB. You cannot compare json values. You can compare text values instead: SELECT * FROM movie_test WHERE tags::text=”[“dramatic”,”women”,”political”]” Note however that values of type JSON are stored as text in a format in which they are given. Thus the result of comparison depends … Read more

Postgresql query for objects in nested JSONB field

You should become familiar with JSON Functions and Operators. — #1 select * from example where content->’Item’->>’Name’ ilike ‘%dog%’ and content->’Item’->>’Spec’ ilike ‘%red%’ — #2 select * from example where content->’Item’->>’Name’ ilike ‘%dog%’ or content->’Item’->>’Spec’ ilike ‘%red%’ — #3 select distinct on(no) t.* from example t, lateral jsonb_each_text(content->’Item’) where value ilike ‘%dog%’; — and select … Read more

How do I search for a specific string in a JSON Postgres data type column?

In Postgres 11 or earlier it is possible to recursively walk through an unknown json structure, but it would be rather complex and costly. I would propose the brute force method which should work well: select * from reports where params::text like ‘%authVar%’; — or — where params::text like ‘%”authVar”%’; — if you are looking … Read more

PostgreSQL rename attribute in jsonb field

In UPDATE use delete (-) and concatenate (||) operators, e.g.: create table example(id int primary key, js jsonb); insert into example values (1, ‘{“nme”: “test”}’), (2, ‘{“nme”: “second test”}’); update example set js = js – ‘nme’ || jsonb_build_object(‘name’, js->’nme’) where js ? ‘nme’ returning *; id | js —-+————————- 1 | {“name”: “test”} 2 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)