
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.
Last updated: April 5, 2025
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.
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.
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.
Here are the main characteristics of JSON data type:
Now, let’s proceed to understanding JSONB and how it’s different from JSON.
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.
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.
Here are the main characteristics of JSONB data type:
Finally, we proceed to understand the key differences between JSON and JSONB.
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.
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.