Sub topic 13
Moving Values Between SQL Types and JSONB
This chapter assumes you know basic SELECT statements, PostgreSQL casts, and the difference between json and jsonb. A cast changes a value from one SQL type to another. JSONB conversion also requires choosing whether a JSON value should remain JSON or become a normal SQL value such as integer or text.
JSONB values and extraction operators
jsonb stores JSON data in a binary format that PostgreSQL can search and index efficiently. Use -> to extract a JSON value while keeping its JSONB type. Use ->> to extract a value as SQL text.
SELECT
'{"n": 12, "s": "34", "active": true}'::jsonb -> 'n' AS json_number,
'{"n": 12, "s": "34", "active": true}'::jsonb ->> 'n' AS text_number,
'{"n": 12, "s": "34", "active": true}'::jsonb ->> 'active' AS text_boolean;The first expression returns the JSONB number 12. The second and third return text values, "12" and "true" without JSON quotation marks.
Use -> when you need to continue navigating through JSON. Use ->> before casting a JSON string to a SQL type.
SELECT
('{"n": 12, "s": "34"}'::jsonb -> 'n')::integer AS from_json_number,
('{"n": 12, "s": "34"}'::jsonb ->> 's')::integer AS from_json_text;The first cast converts a JSONB number directly to integer. The second extracts the JSON string as text and then parses that text as an integer.
Converting SQL values to JSONB
Use to_jsonb to convert a SQL value into JSONB. PostgreSQL preserves useful JSON representations: numbers become JSON numbers, booleans become JSON booleans, and text becomes a JSON string.
SELECT
to_jsonb(42::integer) AS number_value,
to_jsonb(true) AS boolean_value,
to_jsonb('ready'::text) AS string_value;For a row or several named values, use jsonb_build_object. Its keys are text, and its values are converted to appropriate JSON types.
SELECT jsonb_build_object(
'id', 42,
'name', 'Ada',
'enabled', true
) AS document;The result is a JSONB object similar to {"id": 42, "name": "Ada", "enabled": true}. The number and boolean remain native JSON values rather than quoted strings.
Missing keys, nulls, and invalid values
If a key is missing, -> and ->> return SQL NULL. A JSON null also commonly becomes SQL NULL when extracted with ->>. Test required fields before casting them.
SELECT
CASE
WHEN payload ? 'quantity'
AND jsonb_typeof(payload -> 'quantity') = 'number'
THEN (payload ->> 'quantity')::integer
ELSE NULL
END AS quantity
FROM (VALUES
('{"quantity": 3}'::jsonb),
('{"quantity": "three"}'::jsonb),
('{}'::jsonb)
) AS input(payload);A text value such as "three" still fails if cast directly to integer. For PostgreSQL 16 and later, pg_input_is_valid can check text before a cast:
SELECT pg_input_is_valid('42', 'integer') AS valid_number,
pg_input_is_valid('three', 'integer') AS invalid_number;Exercise
Which expression converts the JSON string value "34" into an SQL integer: doc -> 's' or (doc ->> 's')::integer?
Show answer
(doc ->> 's')::integer. The ->> operator extracts the JSON string as text, and the explicit cast parses that text as an integer.