JSON_AGG do?JSON_AGG collects multiple rows into a single JSON array. It helps group data as JSON objects inside one array.
JSON_AGG to aggregate rows from a table?You select the columns you want as JSON objects and wrap them inside JSON_AGG. For example: SELECT JSON_AGG(row_to_json(t)) FROM (SELECT * FROM table_name) t;
JSON_AGG be combined with GROUP BY?Yes. You can group rows by a column and aggregate related rows as JSON arrays for each group.
JSON_AGG and ARRAY_AGG?JSON_AGG returns a JSON array, which can contain JSON objects. ARRAY_AGG returns a SQL array of values, not JSON formatted.
row_to_json inside JSON_AGG?row_to_json converts a table row into a JSON object. Using it inside JSON_AGG creates an array of JSON objects instead of simple values.
JSON_AGG return?JSON_AGG returns a JSON array containing aggregated JSON values.
JSON_AGG to convert rows to JSON objects?ROW_TO_JSON converts a whole row into a JSON object, perfect for use inside JSON_AGG.
JSON_AGG be used with GROUP BY to create grouped JSON arrays?JSON_AGG works well with GROUP BY to aggregate rows per group into JSON arrays.
SELECT JSON_AGG(name) FROM users;This query returns a JSON array containing all user names from the users table.
JSON_AGG compared to ARRAY_AGG?JSON_AGG returns JSON formatted arrays, while ARRAY_AGG returns SQL native arrays.
JSON_AGG to create a JSON array of objects from a table.GROUP BY with JSON_AGG is useful.