2

I have the following sample data for demo:

Table:

create table tbl_json
(
   id json
);

Some values:

insert into tbl_json values('[{"id":1},{"id":2},{"id":3}]');

Query: Convert/cast id into integer from json column.

Tried:

select json_array_elements(id)->>'id'::int ids 
from tbl_json;

Getting an error:

ERROR: invalid input syntax for integer: "id"

0

1 Answer 1

4

The ::int cast is applied to 'id' because it has a higher precedence.

select (json_array_elements(id)->>'id')::int ids 
from tbl_json;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.