PostgreSQL – JSON_BUILD_ARRAY Function

The json_build_array function in PostgreSQL is used to create a JSON array from a list of values. It takes any number of arguments and returns a JSON array containing those values. Each argument will be treated as an element of the resulting JSON array.

Below is the basic syntax of using the json_build_array function:

json_build_array(value1, value2, ..., valueN)

Here’s an example of using json_build_array:

SELECT json_build_array('apple', 42, true);

This query would return a JSON array:

json_build_array 
------------------------ 
["apple", 42, true]

You can also use expressions or columns as arguments to json_build_array:

SELECT json_build_array(first_name, last_name) FROM employees;

This would create a JSON array for each row in the employees table, containing the values from the first_name and last_name columns.

Similar Posts