CodeGym /Courses /SQL SELF /Arrays vs JSONB Comparison

Arrays vs JSONB Comparison

SQL SELF
Level 36, Lesson 0
Available

Today, our goal is to get a deeper understanding of arrays and also compare them more thoroughly with JSONB, check out their strengths and weaknesses, and pick the best practices for using them in real-world tasks.

Arrays vs JSONB: mini data turtles vs flexibility in a box

You probably already know that in PostgreSQL arrays you can store values of any single data type: an array of numbers, strings, or dates. Example: a list of student grades, where all elements are numbers.

-- Table with students and their grades
CREATE TABLE students (
    id SERIAL PRIMARY KEY,
    name TEXT,
    grades INTEGER[]    -- array of grades
);

JSONB, unlike arrays, is about storing data as a JSON structure. This is a lot like a familiar JavaScript object, but with the bonus of fast parsing and indexing. In JSONB you can store both ordered lists and objects with keys and values.

-- Table with students and various data about them
CREATE TABLE students_details (
    id SERIAL PRIMARY KEY,
    name TEXT,
    details JSONB  -- flexible JSON structure
);

Example of data in JSONB:

{
    "grades": [90, 82, 77],
    "address": {
        "city": "Berlin",
        "zip": "352912"
    }
}

So, arrays are a simple way to work with value lists, while JSONB gives you way more options for complex data.

Main differences between arrays and JSONB

Feature Arrays JSONB
Structure type Linear data structure Hierarchical data structure
Element types Only one data type Different data types
Structure size Fixed (linear) Flexible, can include lists and objects
Access speed High with fixed data Slower with complex searches
Indexing Supports indexing well Needs GIN type indexing
Use case Simple list or array of values Complex data: nested objects/lists

Now let’s see how all this works in practice.

When should you use arrays?

Let’s say we have a database with books, and each book can belong to several genres. An array is a good pick here.

CREATE TABLE books (
    id SERIAL PRIMARY KEY,
    title TEXT,
    genres TEXT[]    -- array of genres
);

-- Example of inserting a book with several genres
INSERT INTO books (title, genres)
VALUES ('1984', ARRAY['Dystopia', 'Political Fiction', 'Science Fiction']);

Arrays are great if you’re sure that:

  • your data can be strictly stored as a list,
  • the lists will be small and of the same type (like strings or numbers),
  • you just need to store and fetch lists (no fancy operations).

Advantages of arrays

  • Simple storage of same-type data.
  • Handy for small lists like tags, categories, or grades.

When should you use JSONB?

Now imagine you want to store more complex data about books, including genres, ISBN, and rating. Arrays won’t cut it here — time for JSONB.

CREATE TABLE books_details (
    id SERIAL PRIMARY KEY,
    title TEXT,
    details JSONB    -- book details as JSONB
);

-- Example of inserting complex info about a book
INSERT INTO books_details (title, details)
VALUES (
    '1984',
    '{"genres": ["Dystopia", "Political Fiction", "Science Fiction"],
      "isbn": "9780451524935",
      "rating": 8.9}'
);

JSONB rocks if you need to:

  • store complex or mixed data (numbers, strings, lists, objects),
  • dynamically add parameters without changing the table structure,
  • store nested data (like addresses, features, settings).

Advantages of JSONB

  • Super flexible. You can add new keys and values without changing the table structure.
  • Great for storing complex data, like API JSON responses.

Choosing between arrays and JSONB

If you only need to store lists of same-type data — go with arrays. For example:

-- Storing event participant IDs
CREATE TABLE events (
    id SERIAL PRIMARY KEY,
    participant_ids INTEGER[]
);

If your data is mixed or complex — JSONB is the way to go. For example:

-- Storing client info with addresses
CREATE TABLE customers (
    id SERIAL PRIMARY KEY,
    info JSONB
);

Arrays and JSONB have different approaches to indexing. For arrays, you usually use GIN indexes, and for JSONB — GIN and BTREE, depending on the data structure.

Performance

Arrays are faster for typical search tasks. JSONB is a bit slower, but wins in flexibility. If you just want to search for elements (like genres or IDs), arrays will be quicker:

-- Using arrays with a GIN index
CREATE INDEX idx_genres ON books USING GIN(genres);

-- Filtering books by genre
SELECT * FROM books WHERE genres @> ARRAY['Science Fiction'];

Checking for data presence

JSONB gives you more options for filtering by keys:

-- Checking for the "genres" key
SELECT * FROM books_details WHERE details ? 'genres';

-- Checking for an element in a list
SELECT * FROM books_details WHERE details->'genres' ?| ARRAY['Fantasy', 'Dystopia'];

Arrays let you search for values directly:

-- Checking for an element in an array
SELECT * FROM books WHERE genres @> ARRAY['Fantasy'];

Structure flexibility

JSONB is a lifesaver if your data has complex nested structures:

{
    "genres": ["Fantasy", "Adventure"],
    "ratings": {"goodreads": 8.5, "amazon": 4.7}
}

For arrays, this is impossible without normalization or extra fields.

In the end, arrays and JSONB aren’t rivals — they’re tools for different jobs. If your data looks like a list — use arrays. If you’ve got complex, nested, or mixed data — go for JSONB. The main thing is to keep performance and indexing in mind!

2
Task
SQL SELF, level 36, lesson 0
Locked
Using arrays to store data
Using arrays to store data
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION