Skip to main content

JSON Vs JSONB Storage

Summary: json stores an exact text copy of the input; jsonb stores a parsed binary structure — that one difference explains their speed, indexing, and operator support trade-offs.

At a glance​

QuestionAnswer
What is it?Two PostgreSQL types for storing JSON data
Why does it matter?They differ in storage format, which changes speed, indexing, and which operators work
When do I use it?Almost always jsonb, unless exact input formatting must be preserved

How it works​

jsonjsonb
StorageExact text copy of inputParsed, decomposed binary format
Whitespace/key orderPreserved exactlyNot preserved
Duplicate keysAll stored (last wins on read)Only last duplicate kept
Write speedFaster (no parsing)Slightly slower (parses on insert)
Read/query speedSlower (reparses every query)Faster (already parsed)
GIN indexingNot supportedSupported (@>, ?, etc.)
Operators (||, jsonb_set, etc.)Not supportedSupported

Example​

-- clarify_history column is jsonb, which is why || and jsonb_set work on it
alter table patch_sessions add column clarify_history jsonb;

If that column were json instead, the \|\| concatenation would not work at all — json has no \|\| operator.

Remember​

  • Default to jsonb for nearly all use cases.
  • Use json only when exact byte-for-byte input (whitespace, key order, duplicate keys) must be preserved — rare, mostly audit-logging scenarios.
  • Operators like \|\| and functions like jsonb_set only exist for jsonb.