I have created a table :
CREATE TABLE mytable (
uuid int,
answers jsonb
);
Inserted some JSON values and id :
db.query('insert into mytable(uuid,answers) values($1,$2::jsonb)',
[1,{ "user" : "1", "ans" : "Answer of 1" }])
I am using node js and pg as a client for Postgres.
The data is successfully entered and return the row as expected.
SELECT answers FROM mytable
{"ans": "Answer of 1","user": "1"}
But when I tried to retrieve the value of "ans" of "user" with value 1, it returned an error:
SELECT
elements ->> 'ans'
FROM
mytable,
jsonb_array_elements(answers) elements
WHERE
elements ->> 'user' = '1'
ERROR: cannot extract elements from an object
SQL state: 22023