Skip to content

JSON

Prerequisites:

  • To use the JSON type and all the functions associated with it, you must be connected to a UTF-8 database.
  • While the feature is in beta, you must explicitly enable the feature by setting enable_full_json to ON.

The JSON data type stores variable-length character strings up to 64,000 bytes.

Note that the JSON data type retains an exact copy of the input text. Consequently, processing functions must reparse the JSON on each execution. Additionally, JSON data type does not validate JSON syntax. For syntax validation and better performance, use JSONB instead.

sql
CREATE TABLE t (c JSON);

INSERT INTO t VALUES ('{"c":3, "b":2, "a":1}');

INSERT INTO t VALUES ('{"a":');

SELECT * FROM t;
sql
c
-----------------------
 {"c":3, "b":2, "a":1}
 {"a":
(2 rows)

Comparisons

Comparisons on JSON expressions behave like VARCHAR comparisons. Two JSONs are different if they contain the same keys with the same values in different order. For ordering, the C collation is used.

sql
SELECT '{"a":1,"b":2}'::JSON = '{"b":2,"a":1}'::JSON;
 -- f

SELECT '{"a":1,"b":2}'::JSON < '{"b":2,"a":1}'::JSON;
-- t