JSONB & ArraysBeginner
1Question 1 of 1
sqlWhat is the difference between JSON and JSONB in PostgreSQL?
Explanation & Concept
• `JSON` stores an exact textual representation of the JSON input. It preserves whitespace, object key order, and duplicate keys, but must be reparsed every single time it is queried. • `JSONB` stores JSON in a decomposed binary format. It strips insignificant whitespace, eliminates duplicate keys, does not preserve key order, but is substantially faster to process and supports GIN indexing.
In almost all modern production applications, `JSONB` is the recommended choice.
Code Example
sql
-- Querying and manipulating JSONB
SELECT
id,
metadata->>'company' AS company_name, -- Extract as text (->>)
(metadata->'features'->0)::text AS primary_feature, -- Array indexing
metadata || '{"last_verified": "2026-03-01"}'::jsonb AS merged_json -- Concat update
FROM customers
WHERE metadata ? 'verified_badge'; -- Key existence check (?)How It Works & Key Takeaways
`->` returns a JSON object, while `->>` extracts the value directly as SQL `text`. The `?` operator performs an indexed check to see if a top-level key exists.
Mock Test · 1 Questions · Optional
Ready for Basic CRUD Operations?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner1 questions