0
0
PostgreSQLquery~10 mins

JSON vs JSONB differences in PostgreSQL - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - JSON vs JSONB differences
Store JSON as text
Parse on read
Slower reads
Store JSONB as binary
Parse on write
Faster reads
Supports indexing
JSON stores data as plain text and parses it when reading, while JSONB stores data in a binary format parsed on write, enabling faster reads and indexing.
Execution Sample
PostgreSQL
CREATE TABLE example (
  data_json JSON,
  data_jsonb JSONB
);

INSERT INTO example VALUES
('{"name": "Alice", "age": 30}', '{"name": "Alice", "age": 30}');
Creates a table with JSON and JSONB columns and inserts the same JSON data into both.
Execution Table
StepActionData StoredParsing TimeIndex SupportQuery Speed
1Insert JSON dataText formatNo parsing nowNoN/A
2Insert JSONB dataBinary formatParsing done nowYesN/A
3Select JSON dataText formatParsing on readNoSlower
4Select JSONB dataBinary formatNo parsing on readYesFaster
5Create index on JSONBBinary formatN/AYesFaster queries
6Create index on JSONText formatN/ANoNo speedup
7Query JSONB with indexBinary formatN/AYesFast
8Query JSON without indexText formatParsing on readNoSlow
9End of demo----
💡 Demonstrated JSON stores text parsed on read, JSONB stores binary parsed on write with indexing and faster queries.
Variable Tracker
VariableStartAfter InsertAfter SelectAfter IndexFinal
data_jsonemptytext JSON storedparsed on readno indexslower queries
data_jsonbemptybinary JSONB storedno parse on readindex createdfaster queries
Key Moments - 3 Insights
Why is JSON slower to query than JSONB?
Because JSON stores data as plain text and parses it every time you read it (see execution_table rows 3 and 8), while JSONB parses once on insert and stores in binary for faster access.
Can you create indexes on JSON columns?
No, JSON columns do not support indexing (execution_table rows 5 and 6). Only JSONB supports indexes, which speeds up queries.
Does JSONB store data exactly like JSON text?
No, JSONB stores data in a binary format that is not text but represents the same data structure, enabling faster operations (execution_table rows 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is JSONB data parsed?
AStep 3 - Select JSON data
BStep 1 - Insert JSON data
CStep 2 - Insert JSONB data
DStep 4 - Select JSONB data
💡 Hint
Check the 'Parsing Time' column for JSONB insert step in execution_table row 2.
According to variable_tracker, what happens to data_jsonb after index creation?
AIt remains unindexed and slow
BAn index is created and queries become faster
CIt is parsed on every read
DIt converts back to text format
💡 Hint
Look at the 'After Index' and 'Final' columns for data_jsonb in variable_tracker.
From the execution_table, why is querying JSON slower than JSONB?
ABecause JSON is parsed on read every time
BBecause JSON supports indexing
CBecause JSON is stored in binary
DBecause JSONB stores text
💡 Hint
See the 'Parsing Time' and 'Query Speed' columns for JSON in execution_table rows 3 and 8.
Concept Snapshot
JSON stores data as plain text and parses it on every read.
JSONB stores data in a binary format parsed once on write.
JSONB supports indexing, making queries faster.
Use JSONB for better performance and indexing.
Use JSON if you need exact text preservation.
Full Transcript
This visual execution compares JSON and JSONB in PostgreSQL. JSON stores data as text and parses it each time you read it, which slows queries. JSONB stores data in a binary format parsed once when inserted, enabling faster reads and supporting indexes. The execution table shows steps inserting and selecting data, highlighting parsing times and indexing support. Variable tracking shows how data_json remains text and unindexed, while data_jsonb is binary and indexed. Key moments clarify why JSON is slower, why JSONB supports indexes, and how JSONB stores data differently. The quiz tests understanding of parsing steps, indexing effects, and query speed differences. The snapshot summarizes key points: JSON is text and slower, JSONB is binary, indexed, and faster. Choose JSONB for performance and indexing, JSON for exact text needs.