JSONB_PRETTY

Pretty-print a JSONB value as text with indentation and line breaks.

Category: jsonReturns: STRINGDialect: PostgreSql

Syntax

JSONB_PRETTY(jsonb)

Description

## Overview Returns a human-readable TEXT representation of a JSONB value with indentation and line breaks. Intended for debugging, UI display, and exporting to files where readability is more important than compactness. Keys are emitted in JSONB canonical form order, which is deterministic across runs but may differ from the original input text order. Indentation is four spaces per nesting level. ## Behavior - Returns TEXT, not JSONB. - Uses four-space indentation per level. - Key order follows JSONB canonical form (typically alphabetical), not insertion order. - Returns NULL for NULL input. - Output is a superset of the compact form; parsing the result reproduces the original JSONB. ## Compatibility - Output is valid JSON per RFC 8259. - Matches the jsonb_pretty convention in widely adopted SQL implementations.

Parameters

NameTypeDescription
jsonbSpecifies the JSONB document to format with indentation and line breaks. Returns NULL for NULL input.

Examples

-- Pretty-print a simple object
SELECT JSONB_PRETTY('{"name": "Alice", "age": 30}'::JSONB);
-- Pretty-print a nested structure
SELECT JSONB_PRETTY('{"user": {"id": 1, "roles": ["admin", "editor"]}}'::JSONB);
-- Pretty-print an array
SELECT JSONB_PRETTY('[1, 2, 3]'::JSONB);
-- NULL input returns NULL
SELECT JSONB_PRETTY(NULL);
-- Realistic: inspect a row's payload when debugging
SELECT event_id, JSONB_PRETTY(payload) AS payload_pretty
FROM analytics.telemetry.events
WHERE event_id = 'evt-abc-123';

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →