15

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?

1
  • 4
    jsonb[] is not an "extra" datatype, it's simply an array of JSONB values. Similar to text[] or integer[]. You can create arrays from every type. But I do agree that using an array jsonb hardly ever makes sense. Commented Jul 15, 2020 at 14:44

1 Answer 1

13

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.

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.