JSON input and output syntax

Learn about JSON input and output syntax.

The input/output syntax for the JSON data types is as specified in RFC 7159.

Examples of valid json and jsonb expressions:
  • Simple scalar/primitive value

    Primitive values can be numbers, quoted strings, true, false, or null.

    SELECT '5'::json;
  • Array of zero or more elements (elements need not be of same type)
    SELECT '[1, 2, "foo", null]'::json;
  • Object containing pairs of keys and values
    Note that object keys must always be quoted strings.
    SELECT '{"bar": "baz", "balance": 7.77, "active": false}'::json;
  • Arrays and objects can be nested arbitrarily
    SELECT '{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}'::json;
When a JSON value is input and then printed without any additional processing, json outputs the same text that was input. jsonb does not preserve semantically-insignificant details (e.g. whitespace).
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::json;
                      json                       
-------------------------------------------------
 {"bar": "baz", "balance": 7.77, "active":false}
(1 row)

SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb;
                      jsonb                       
--------------------------------------------------
 {"bar": "baz", "active": false, "balance": 7.77}
(1 row)
Note: In jsonb, numbers are printed according to the behavior of the underlying numeric type. The numbers entered with E notation will be printed without it. jsonb preserves trailing fractional zeroes even though those are semantically insignificant for purposes such as equality checks.
Example:
SELECT '{"reading": 1.230e-5}'::json, '{"reading": 1.230e-5}'::jsonb;
         json          |          jsonb          
-----------------------+-------------------------
 {"reading": 1.230e-5} | {"reading": 0.00001230}
(1 row)