Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of JSONB[] (JSONB ARRAY) data type in PostgreSQL?

I am using the Postgres database in one of the projects. Now I have a requirement to store JSON array in a database. Something like below:

For e.g., I have below JSON structure:

[
  {
    "Id": 1,
    "Name": "XYZ"
  },
  {
    "Id": 2,
    "Name": "ABC"
  }
]

For this purpose, I am using JSONB datatype and it works fine. Then what it is the use of JSONB[] datatype when we can simply store a JSON array in JSONB datatype?

like image 483
Sunny Avatar asked Jan 29 '26 04:01

Sunny


1 Answers

In my opinion you are doing it correctly.

The jsonb[] datatype stores multiple jsonb objects in a PostgreSQL array.

In your case you can access all the array elements using the jsonb functions and operators.

If you decomposed that array into its constituent objects and stored each as an element in jsonb[], then you would need to use a mix of PostgreSQL array functions and operators and jsonb functions and operators to act on the data's contents.

like image 182
Mike Organek Avatar answered Jan 31 '26 06:01

Mike Organek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!