0
create table test (jsonarraycolumn jsonb[]);

insert into test values (array['{"a": 45.50, "b": 10}', '{"a": 70.50, "b": 80}']);

I have created a table 'test'.

Trying to insert an array of json:

1st json:

'{"a": 45.50, "b": 10}'

2nd json:

'{"a": 70.50, "b": 80}'

I am getting the error:

SQL Error [42804]: ERROR: column "jsonarraycolumn" is of type jsonb[] but expression is of type text[]
  Hint: You will need to rewrite or cast the expression.
  Position: 26

How is an array of json inserted into a postgresql column?

2
  • 2
    Do you really want jsonb[]? It's a better practice to declare the column as jsonb, and then store an array json value in it: '[{"a": 45.50, "b": 10}, {"a": 70.50, "b": 80}'] Commented Jun 3, 2024 at 7:23
  • Does this answer your question? What is the use of JSONB[] (JSONB ARRAY) data type in PostgreSQL? Commented Jun 3, 2024 at 11:57

1 Answer 1

1

The type of array['...', '...'] is text[]. You need to cast it to jsonb[], just like the error message says:

insert into test values (array['{"a": 45.50, "b": 10}', '{"a": 70.50, "b": 80}']::jsonb[]);
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.