Skip to content

JSON_OBJECTAGG

  • key: The key parameter indicates the column or expression that will be used as keys in the JSON object. Every JSON object entry comprises a key-value pair, with this parameter defining what will be used as the key.
  • value: The value parameter specifies the column or expression that will be used as values in the JSON object. This is the information the key will map to in the resulting JSON object.
SELECT JSON_OBJECTAGG(
employee_id,
CONCAT(first_name, ' ', last_name)
)
FROM employees
WHERE employee_id <= 5;
{
"1": "John Doe",
"2": "Jane Smith",
"3": "Mary Johnson",
"4": "James Brown",
"5": "Patricia Davis"
}

JSON_OBJECTAGG is a MySQL function that concatenates row values into a JSON object. In this example, it outputs a JSON object where each key is an employee ID, and the corresponding value is the full name of the employee. Only records with an employee ID less than or equal to 5 are selected.