JSON vs JSONB in PostgreSQL: Differences, Indexing and Which to Use

7 min·Nov 22, 2026
AI Summary

In PostgreSQL, json stores the input text verbatim (whitespace, key order and duplicate keys preserved) and re-parses it on every access. jsonb stores a decomposed binary form: whitespace is dropped, key order is not preserved, and only the last duplicate key is kept. jsonb is slightly slower to insert but much faster to query, and supports containment (@>), key-existence (?, ?|, ?&) operators and GIN indexes. Use jsonb by default; use json only when you must keep the exact original text.

MongoDB $jsonSchema Validation
A MongoDB collection validator using $jsonSchema to enforce required fields, types and enums on a users collection — schema safety without an ORM.
{
  "$jsonSchema": {
    "bsonType": "object",
    "required": [
      "email",
      "createdAt"
    ],
    "additionalProperties": false,
    "properties": {
      "_id": {
        "bsonType": "objectId"
      },
      "email": {
        "bsonType": "string",
        "pattern": "^.+@.+$",
        "description": "must be a valid email and is required"
      },
      "role": {
        "bsonType": "string",
        "enum": [
          "admin",
          "member",
          "viewer"
        ],
        "description": "must be one of the allowed roles"
      },
      "age": {
        "bsonType": "int",
        "minimum": 0,
        "maximum": 120
      },
      "createdAt": {
        "bsonType": "date"
      }
    }
  }
}

PostgreSQL has two column types for JSON. They accept the same input but store it very differently.

The short version

jsonjsonb
Stored asExact input textParsed binary
WhitespacePreservedRemoved
Key orderPreservedNot preserved
Duplicate keysAll keptLast one wins
Insert speedFaster (just validates)Slightly slower (parses and converts)
Query speedRe-parses on every accessFast
Containment / existence operatorsNo@>, <@, ?, ?|, ?&
GIN indexingNoYes
Equality comparisonNoYes

Use jsonb unless you need the exact original text.

See the difference

SELECT '{"b": 1, "a": 2, "a": 3}'::json AS j, '{"b": 1, "a": 2, "a": 3}'::jsonb AS jb; j | {"b": 1, "a": 2, "a": 3} jb | {"a": 3, "b": 1}

json returns the text unchanged. jsonb drops whitespace, reorders keys and keeps only the last "a".

A jsonb table

CREATE TABLE employees ( id bigint PRIMARY KEY, data jsonb NOT NULL ); INSERT INTO employees VALUES (1001, '{"name": "Asha Rao", "department": "Engineering", "skills": ["go", "kubernetes"], "address": {"city": "Bengaluru"}}'), (1002, '{"name": "Daniel Okafor", "department": "Engineering", "skills": ["typescript"], "address": {"city": "Lagos"}}');

Querying

-- Read a field as text SELECT data->>'name' FROM employees; -- Navigate nested objects SELECT data->'address'->>'city' FROM employees; SELECT data #>> '{address,city}' FROM employees; -- Containment: employees in Engineering who know Go SELECT id FROM employees WHERE data @> '{"department": "Engineering", "skills": ["go"]}'; -- Key existence SELECT id FROM employees WHERE data ? 'manager'; -- SQL/JSON path SELECT jsonb_path_query(data, '$.skills[*]') FROM employees;

Updating

-- Set one nested field UPDATE employees SET data = jsonb_set(data, '{address,city}', '"Mumbai"') WHERE id = 1001; -- Merge fields (right side wins) UPDATE employees SET data = data || '{"isActive": true}'; -- Remove a key UPDATE employees SET data = data - 'manager';

Indexing

-- Whole-document GIN index: speeds up @>, ?, ?|, ?& CREATE INDEX employees_data_gin ON employees USING gin (data); -- Smaller GIN index that only supports @> (and jsonpath @? / @@) CREATE INDEX employees_data_path ON employees USING gin (data jsonb_path_ops); -- One hot field: a plain B-tree expression index CREATE INDEX employees_department ON employees ((data->>'department'));

Filters must use the same operator or expression the index was built for. WHERE data->>'department' = 'Engineering' uses the expression index, not the GIN index.

When json is the right choice

  • You store raw webhook or API payloads and must reproduce them byte for byte, for example to verify a signature.
  • You only ever store and return the document and never query inside it.

Design tips

  • Put fields you filter, join or sort on in real columns; keep jsonb for the flexible remainder.
  • Add a CHECK constraint (or validate with JSON Schema in the application) so bad shapes do not sneak in.
  • Very large jsonb values are TOASTed and compressed; updating one key rewrites the whole value, so avoid huge, frequently updated documents.

Related

FAQ

Should I use json or jsonb in PostgreSQL?

jsonb for almost everything. Choose json only if you must return the document exactly as it was sent, including whitespace, key order and duplicate keys (for example an audit log of raw webhook payloads).

What is the difference between -> and ->>?

-> returns the value as json/jsonb (so you can keep navigating), ->> returns it as text. data->'address'->>'city' gets the city as text.

Can I index a jsonb column?

Yes. A GIN index on the whole column speeds up @>, ?, ?| and ?& queries. For one frequently filtered field, a B-tree expression index on (data->>'field') is smaller and faster.

Does jsonb keep key order?

No. jsonb stores keys in its own internal order, and duplicate keys collapse to the last one. If key order matters to you, either use json or keep an explicit array.