DROP PROCEDURE IF EXISTS clientsActivities;
DELIMITER //
CREATE PROCEDURE clientsActivities() 
BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE indx INTEGER DEFAULT 0;
    DECLARE client_id VARCHAR(4000);
    DECLARE activities VARCHAR(4000);
    DECLARE currentClient CURSOR FOR SELECT `clients`.`id` AS 'client_id', JSON_EXTRACT(REPLACE(`clients`.`activities`, '&quot;', ''), '$') FROM `clients` WHERE `clients`.`activities` != '' AND `clients`.`activities` IS NOT NULL AND `clients`.`activities` != 'null' AND `clients`.`activities` NOT LIKE '[]';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    OPEN currentClient;
    TRUNCATE TABLE `clients_activities`;
    getClients: LOOP
        FETCH currentClient INTO client_id, activities;
        IF finished = 1 THEN LEAVE getClients;
        END IF;
        WHILE indx < JSON_LENGTH(activities) DO
            SELECT client_id, JSON_EXTRACT(activities, CONCAT('$[', indx, ']')) AS activity_id;
            INSERT INTO `clients_activities` (`client_id`, `activity_id`) VALUES (client_id, JSON_EXTRACT(activities, CONCAT('$[', indx, ']')));
            SET indx = indx + 1;
        END WHILE;
        SET indx = 0;
    END LOOP getClients;
    CLOSE currentClient;
END; //
DELIMITER ;
CALL clientsActivities();