Sunny
Sunny

Reputation: 932

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?

Upvotes: 13

Views: 20315

Answers (1)

Mike Organek
Mike Organek

Reputation: 12484

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.

Upvotes: 12

Related Questions