Sub topic 1
json versus jsonb: how each type stores data
PostgreSQL provides two types for storing JSON documents: json and jsonb. Both accept JSON objects, arrays, strings, numbers, booleans, and null. The important difference is how PostgreSQL stores and processes the value after it receives it.
This chapter assumes you know basic CREATE TABLE, INSERT, SELECT, and PostgreSQL data types. You should also know that a JSON object contains key-value pairs and that a JSON array contains ordered values.
Prerequisites
You need:
- Basic SQL, including
SELECT,INSERT, andCREATE TABLE - Basic PostgreSQL types such as
text,integer, andnumeric - Familiarity with JSON objects and arrays
- A PostgreSQL instance, preferably version 12 or later
The two storage models
The json type stores the original JSON text. PostgreSQL validates that the text is valid JSON when you insert it, but it keeps the document's formatting. Whitespace and the order of object keys remain available when PostgreSQL returns the value.
The jsonb type stores a decomposed binary representation. PostgreSQL parses the document during insertion and stores its structure rather than its original text. This removes insignificant formatting and makes later searches and navigation faster.
| Property | `json` | `jsonb` |
|---|---|---|
| Stores original formatting | Yes | No |
| Preserves object key order | Yes | No |
| Keeps duplicate object keys | Yes, in the stored text | No, the last value wins |
| Processing and querying | Usually slower | Usually faster |
| Supports GIN indexes | No | Yes |
| Insert conversion cost | Lower | Slightly higher |
For example, these two documents are different strings:
{"a": 1, "b": 2}{ "b": 2, "a": 1, "a": 9 }With json, PostgreSQL preserves the submitted text. With jsonb, the duplicate a key is discarded and the later value, 9, is retained. Object key order and extra spaces do not carry meaning in jsonb.
Choosing between json and jsonb
Use jsonb for most application data. It is the practical choice when you will filter, extract, update, or index values inside documents. Its structure lets PostgreSQL use JSON operators and indexes efficiently.
Use json when preserving the exact input text is part of the requirement. Examples include storing an external payload for auditing, retaining a signed document's representation, or delaying interpretation to another system. Even then, consider storing both the original text and a jsonb version if you need exact archival and efficient querying.
The storage choice is a data contract. Changing from json to jsonb can lose formatting, key order, and duplicate keys, so do not treat the types as interchangeable when the original representation matters.
CREATE TEMP TABLE document_examples (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
original json,
searchable jsonb
);
INSERT INTO document_examples (original, searchable)
VALUES (
'{ "b": 2, "a": 1, "a": 9 }',
'{ "b": 2, "a": 1, "a": 9 }'
);
SELECT original, searchable
FROM document_examples;The original column can display the submitted spacing, order, and duplicate key. The searchable column represents the parsed document. Its output can have different spacing and ordering, and its a value is 9.
A json value is still validated. It is not an unvalidated text column. However, every operation that needs to inspect its structure may need to parse the stored text. Repeated queries over large documents can therefore cost more than equivalent operations on jsonb.
A simple decision rule
Ask what the database must do with the value:
- If the database mainly archives and returns the exact payload, consider
json. - If the database searches inside the document, use
jsonb. - If queries need containment tests or indexes, use
jsonb. - If the document has stable fields that are frequently filtered or joined, consider ordinary relational columns instead.
- If only some fields vary, keep important stable fields as columns and use
jsonbfor the variable part.
The choice between json and jsonb comes before choosing operators or indexes. Those later features depend on having a representation that supports the workload.
Exercise
An application receives JSON webhook payloads. Users must later search by nested status and customer fields, but auditors also require the exact original payload. Which design fits these requirements?
Show answer
Store the exact payload in a json or text archive column and store a jsonb copy for searching. If exact preservation is unnecessary, use only jsonb.