JSON and JSONB are two ways to store JSON data in PostgreSQL. Knowing their differences helps you choose the best one for your needs.
JSON vs JSONB differences in PostgreSQL
CREATE TABLE example_json ( data JSON ); CREATE TABLE example_jsonb ( data JSONB );
JSON stores data as plain text, preserving formatting.
JSONB stores data in a binary format, optimized for searching and indexing.
INSERT INTO example_json (data) VALUES ('{"name": "Alice", "age": 30}');
INSERT INTO example_jsonb (data) VALUES ('{"name": "Alice", "age": 30}');
SELECT data->>'name' FROM example_jsonb WHERE data @> '{"age": 30}';
This example shows creating tables with JSON and JSONB columns, inserting data, and querying each type. JSON query uses text matching, JSONB query uses JSON containment operator.
CREATE TABLE people_json ( info JSON ); CREATE TABLE people_jsonb ( info JSONB ); INSERT INTO people_json (info) VALUES ('{"name": "Bob", "city": "NY"}'), ('{"name": "Carol", "city": "LA"}'); INSERT INTO people_jsonb (info) VALUES ('{"name": "Bob", "city": "NY"}'), ('{"name": "Carol", "city": "LA"}'); -- Query JSON data (text search) SELECT info->>'name' AS name FROM people_json WHERE info::text LIKE '%NY%'; -- Query JSONB data (index search) SELECT info->>'name' AS name FROM people_jsonb WHERE info @> '{"city": "NY"}';
JSONB supports indexing, which makes queries faster.
JSON preserves the exact text, including spaces and order of keys.
JSONB removes duplicate keys and orders keys internally.
JSON stores data as plain text, keeping formatting.
JSONB stores data in binary, optimized for speed and indexing.
Use JSONB when you need fast queries and updates on JSON data.
Practice
json and jsonb data types in PostgreSQL?Solution
Step 1: Understand storage formats
jsonstores JSON data as plain text, preserving the original formatting including whitespace.Step 2: Understand
jsonbstoragejsonbstores JSON data in a binary format optimized for faster processing and indexing.Final Answer:
jsonstores data as plain text, whilejsonbstores data in a binary format. -> Option AQuick Check:
Storage format difference = Option A [OK]
- Thinking jsonb preserves whitespace like json
- Assuming jsonb cannot index data
- Believing jsonb cannot store nested objects
jsonb type in PostgreSQL?Solution
Step 1: Review PostgreSQL syntax for JSONB
The correct data type name isjsonb(case-insensitive), used directly as a column type.Step 2: Check each option
Option A usesJSONBcorrectly. Option A tries to declare an array of jsonb, which is valid but not the simplest declaration. Option A usesJSONtype, notjsonb. Option A uses a non-existent typeBINARYJSON.Final Answer:
CREATE TABLE data (info JSONB); -> Option AQuick Check:
Correct jsonb column syntax = Option A [OK]
- Confusing JSON and JSONB types
- Using invalid type names like BINARYJSON
- Adding unnecessary array brackets
items with a json column data storing '{"a":1, "b":2}', what will this query return?SELECT data @> '{"a":1}'::jsonb FROM items;Solution
Step 1: Understand operator compatibility
The@>operator is defined forjsonbtypes, not forjson. Using it on ajsoncolumn causes an error.Step 2: Analyze the query
The query tries to use@>ondatawhich isjson, but the right side is cast tojsonb. This mismatch causes a type error.Final Answer:
Error becausedatais json, but query uses jsonb operator -> Option AQuick Check:
jsonb operator on json column = Error [OK]
- Assuming json and jsonb are interchangeable
- Ignoring type casting errors
- Expecting operator to work on json type
jsonb column but your query is slow when filtering by keys. What is the most likely cause?Solution
Step 1: Understand indexing for jsonb
jsonbsupports GIN or BTREE indexes to speed up key/value lookups.Step 2: Identify cause of slow queries
If no index exists on thejsonbcolumn, queries filtering by keys must scan all rows, causing slowness.Final Answer:
No GIN or BTREE index on thejsonbcolumn -> Option AQuick Check:
Missing index on jsonb = slow queries [OK]
- Confusing json and jsonb types
- Using LIKE on jsonb instead of jsonb operators
- Ignoring indexing for jsonb columns
Solution
Step 1: Choose data type for fast queries
jsonbis optimized for querying and indexing nested JSON data.Step 2: Use appropriate indexing
Creating a GIN index on thejsonbcolumn enables fast key and path lookups.Final Answer:
Store data asjsonband create a GIN index on the column -> Option BQuick Check:
jsonb + GIN index = best for fast nested queries [OK]
- Using json type which is slower for queries
- Avoiding indexes to speed inserts but hurting queries
- Parsing JSON outside database causing overhead
Ready to prove it?
Pass the test and this pattern moves unknown
