Baeldung Pro – SQL – NPI EA (cat = Baeldung on SQL)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Introduction

PostgreSQL offers powerful support for storing and querying JSON data. This makes it a compelling choice for applications that require flexible schema-less storage. With the introduction of JSONB in PostgreSQL 9.4, it makes it easy to gain an even more optimized way to work with JSON data.

While both JSON and JSONB store structured data in JSON format, their internal storage mechanisms and performance characteristics differ significantly. Furthermore, understanding the differences between JSON and JSONB data types is important for selecting the most efficient option based on specific use cases.

In this tutorial, we’ll explore JSON and JSONB in PostgreSQL, highlighting their advantages, use cases, and key differences to help in making informed decisions.

2. JSON Data Type

The JSON data type stores data in its original text format, preserving the exact structure, whitespace, and key ordering of the input. In particular, any JSON document inserted into a JSON column remains unchanged when retrieved.

2.1. Practical Example

For illustration, let’s consider a table that stores user profile information in JSON format:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    profile JSON
);
CREATE TABLE
Time: 0.031s

Next, we insert a JSON document into the created table:

INSERT INTO users (profile) 
 VALUES ('{"name": "Alice", "age": 30, "preferences": {"theme": "dark", "notifications": true}}');
INSERT 0 1
Time: 0.003s

Finally, let’s retrieve the JSON data from the table:

SELECT profile FROM users;
+---------------------------------------------------------------------------------------+
| profile                                                                               |
|---------------------------------------------------------------------------------------|
| {"name": "Alice", "age": 30, "preferences": {"theme": "dark", "notifications": true}} |
+---------------------------------------------------------------------------------------+
SELECT 1
Time: 0.013s

Since JSON stores data as text, querying specific values requires parsing the JSON structure at runtime:

SELECT profile->>'name' AS name FROM users;
+-------+
| name  |
|-------|
| Alice |
+-------+
SELECT 1
Time: 0.005s

This query extracts the name field from the JSON object. However, because JSON is stored as text, each query must parse the document. This can impact performance when working with large datasets.

2.2. Characteristics

Here are the main characteristics of JSON data type:

  • They’re stored in plain text ensuring the input format remains intact
  • Whitespace, key ordering, and duplicate keys are retained
  • JSON does not support indexing, making queries slower when searching for specific values inside JSON objects
  • PostgreSQL ensures that only valid JSON documents are stored

Now, let’s proceed to understanding JSONB and how it’s different from JSON.

3. JSONB Data Type

The JSONB type is a binary representation of JSON. Further, it offers enhanced performance and additional functionalities compared to the standard JSON type. Unlike JSON, which stores data as plain text, JSONB processes and stores JSON data in a binary format, optimizing storage and query execution.

3.1. Practical Example

Again, let’s create a similar table, but this time using JSONB:

CREATE TABLE users_jsonb (
    id SERIAL PRIMARY KEY,
    profile JSONB
);
CREATE TABLE
Time: 0.012s

Next, we insert a JSON document into the table:

INSERT INTO users_jsonb (profile)
 VALUES ('{"name": "Alice", "age": 30, "preferences": {"theme": "dark", "notifications": true}}');
INSERT 0 1
Time: 0.004s

Finally, let’s retrieve the JSONB data from the created table:

SELECT profile FROM users_jsonb;
+---------------------------------------------------------------------------------------+
| profile                                                                               |
|---------------------------------------------------------------------------------------|
| {"age": 30, "name": "Alice", "preferences": {"theme": "dark", "notifications": true}} |
+---------------------------------------------------------------------------------------+
SELECT 1
Time: 0.005s

Now, let’s extract a specific value from the JSONB table:

SELECT profile->>'name' AS name FROM users_jsonb;
+-------+
| name  |
|-------|
| Alice |
+-------+
SELECT 1
Time: 0.008s

Unlike JSON, JSONB is stored in a decomposed binary format, making queries more efficient.

3.2. Characteristics

Here are the main characteristics of JSONB data type:

  • It stores data in a binary format, which makes it more efficient for processing and querying
  • Unlike JSON, JSONB doesn’t retain whitespace, key ordering, or duplicate keys
  • JSONB allows the creation of indexes on JSON fields, significantly improving query performances
  • Queries on JSONB data are generally faster than JSON due to its indexed storage

Finally, we proceed to understand the key differences between JSON and JSONB.

4. Differences Between JSON and JSONB Data Type

Let’s look at the key differences between JSON and JSONB data types:

JSON JSONB
It stores data as plaintext, exactly as input, which includes whitespace and key order It stores data in a decomposed binary format, which eliminates whitespace and reorders keys
It’s slower because the database must re-parse the text every time it is accessed It’s faster because the data is pre-parsed into binary format
It’s slightly faster to insert since no conversion is needed It’s slightly slower to insert due to binary conversion overhead
It doesn’t support efficient indexing (full scans are required for searches) It supports GIN (Generalized Inverted Index) for efficient querying on keys and values
Preserves the original order of keys as inserted Does not preserve key order; optimizes for storage and query efficiency
It retains duplicate keys (last occurrence takes precedence in queries) It automatically removes duplicates, keeping only the last occurrence
It’s typically larger because it retains formatting (spaces, indentation) It’s more compact due to binary optimization (no extra whitespace)
Best when input order/format must be preserved (for example, logging raw JSON) Preferred for most applications due to faster queries, indexing, and smaller size

This table highlights the key differences between the two data types in PostgreSQL. However, JSONB generally outperforms JSON in most use cases because it offers better performance and supports indexing.

5. Conclusion

In this article, we’ve explored the differences between JSON and JSONB in PostgreSQL, highlighting their storage mechanisms and performance characteristics. JSONB offers superior query efficiency and indexing capabilities, making it the preferred choice for most applications. Understanding these distinctions enables developers to optimize database performance and storage based on specific requirements.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.