PostgreSQL extracts the keys from JSONB, “Unable to call JSONB_Object_Keys on scalar”

I want to use jsonb to solve the problem in Postgres. There are many problems here, what I want to do is:

SELECT table.column->>'key_1' as a FROM "table"

I tried -> there are some combinations of brackets, but I always get a zero.

So I tried to get all the keys first and see if it even recognizes jsonb.

SELECT jsonb_object_keys(table.column) as a FROM "table"

This An error was raised:

cannot call jsonb_object_keys on a scalar

So, check the column type (I created it, so I know it is jsonb, but Anyway)

SELECT pg_typeof(column) as a FROM "table" ORDER BY "table"."id" ASC LIMIT 1

This is correct Gave me the result of “jsonb”.

The values ​​in the column are similar to {“key_1”: “New York”, “key_2”: “key value”, “key_3”: “United States”}

So, I am really confused what is actually happening here, why does it call my json data a scalar? What does it actually mean and how to solve this problem?

Any help in this regard will be very helpful.

PS: I am using rails, post this as a general question of the problem. Any track-specific solution will also work .

So the problem turned out to be only SQL instead of SQL.

As I mentioned, I use rails (5.1), and I used the default value'()’ for the jsonb column. I used a bidirectional serializer for the column by defining it in the model of the table.

Removing this serializer and adjusting the default value to () actually solved the problem.

I think my serializer is operating on the value, but it is still in the database, it has As I mentioned the correct value in the question.

I still don’t know 100% what the problem is. But it has been solved anyway. If anyone can shed some light on what the problem is, then That’s great.

Hope this helps someone.

I want to use jsonb in Postgres to solve the problem. There are many problems here, I What I want to do is:

SELECT table.column->>'key_1' as a FROM "table"

I tried –> There are also some combinations of parentheses, but I always get a zero.

So I tried to get all the keys first and see if it even recognizes jsonb.

< pre>SELECT jsonb_object_keys(table.column) as a FROM “table”

This caused an error:

cannot call jsonb_object_keys on a scalar

pre>

So, check the column type (I created it, so I know it is jsonb, but anyway)

SELECT pg_typeof(column) as a FROM "table "ORDER BY "table"."id" ASC LIMIT 1

This correctly gives me the result of "jsonb".

The value in the column is similar to {"key_1": "New York", "key_2": "key value", "key_3": "United States"}

So, I really I am very confused about what is actually happening here, why does it call my json data a scalar? What does it actually mean and how to solve this problem?

Any help in this regard will be very helpful.

PS: I am using rails, post this as a general question of the problem. Any track-specific solution will also work .

So the problem turned out to be only SQL instead of SQL.

As I mentioned, I use rails( 5.1), I used the default value'{}' as the jsonb column. I used a bidirectional serializer for the column by defining it in the model of the table.

Remove this serializer and set the default value Adjusting to () actually solved the problem.

I think my serializer is manipulating the value, but it is still in the database, and it has the correct value as I mentioned in the question.< /p>

I still don’t know 100% what the problem is. But it has been solved anyway. It would be great if anyone could understand what the problem is.

Hope this helps someone.

Leave a Comment

Your email address will not be published.