Understanding the new JSON capabilities in Postgres 16
Learn about the latest new features introduced in Postgres 16 for working with JSON data
We’re excited to announce that Neon now supports Postgres 16. This latest release includes several performance improvements and developer experience enhancements. One of the most anticipated features is the expanded support for SQL/JSON syntax, including:
JSON_ARRAY()
: Constructs a JSON array.JSON_ARRAYAGG()
: Aggregates input values into a JSON array.IS JSON
: A predicate to determine if a given value is a valid JSON.
In this article, we will cover these new functions and predicates that you can try out on Neon today.
JSON_ARRAY
The json_array()
function is designed for constructing a JSON array. There are two main usages:
From a series of values:
Output:
From the outcome of a SELECT
query:
Output:
JSON_ARRAYAGG
The json_arrayagg()
function essentially behaves like the json_array()
function but is designed to operate as an aggregate function. To understand the difference between json_arrayagg()
and json_array()
functions, let’s consider the following example.
Using json_array()
, the query will return a list of arrays that include the last names:
Output:
However, with json_arrayagg()
, the query returns one array of all last names:
Output:
IS JSON Predicate
The IS JSON
predicate is introduced to test if an expression can be parsed as JSON and possibly of a specified type.
Testing various JSON types:
Example:
Output:
Testing arrays with unique keys:
Example:
Output:
Conclusion
Postgres 16 introduces many improvements and features, some of which were contributed by the Neon Postgres team (Heikki Linnakangas, Matthias van de Meent, Tristan Partin). We’re excited about this new version, which you can try it out on Neon today.
If you have any questions or feedback, please reach out to us in our community forum. We’d love to hear from you.
Also, make sure to subscribe below if you would like to be notified of new content we publish on our blog.