Junior — Middle
What is the main difference between JSON and JSONB data types in PostgreSQL database?
sobes.tech AI
Answer from AI
The main difference between JSON and JSONB data types in PostgreSQL is in how data is stored and processed:
- JSON stores data as plain text, exactly as input. Each time data is read, it must be parsed, which can slow down operations.
- JSONB (Binary JSON) stores data in a binary format, allowing faster search, indexing, and filtering operations.
JSONB supports indexes, which improve query performance, and it stores data more efficiently by removing whitespace and ordering keys.
Example of using JSONB:
CREATE TABLE example (
data JSONB
);
INSERT INTO example VALUES ('{"name": "Alice", "age": 30}');
SELECT * FROM example WHERE data->>'name' = 'Alice';
If performance and frequent JSON data queries are important, JSONB is preferred. If preserving original format and order is needed, JSON is suitable.