How to get a JSON array without JSON_ARRAYAGG in MySQL
Finally I've found a solution. I create a string array using CONCAT and GROUP_CONCAT functions and then cast it to JSON.
SELECT JSON_REPLACE(
    data,
    '$.numbers',
    IF(
      (
        SELECT COUNT(number) FROM numbers
        WHERE JSON_CONTAINS(data, CAST(numbers.id AS CHAR), '$.numbers') = 1
      ) &gt; 0,
      (
        SELECT CAST(CONCAT('[', GROUP_CONCAT(CONCAT('&quot;', number, '&quot;')), ']') AS JSON) FROM numbers
        WHERE JSON_CONTAINS(data, CAST(numbers.id AS CHAR), '$.numbers') = 1
      ),
      NULL
    )
) AS json FROM foo;