Iterate through JSON in MySQL query
<p>I'm looking for something like <code>forEach</code> for a JSON array in MySQL.</p>

<p>I manager IDs in <a href="https://dev.mysql.com/doc/refman/5.7/en/json.html" rel="nofollow">MySQL JSON data type</a> like this: <code>[1, 2, 3, 4, 5]</code>, and I want to perform an action for each item in the list.</p>

<p>A naive solution is to do a WHILE loop with a counter that starts at 0 and ends when VAR_MANAGER_ID is null. Here is a contrived example of how the inside of the WHILE loop would look:</p>

<pre><code>SET VAR_PATH = CONCAT('$[', COUNTER, ']');
SET VAR_MANAGER_ID = JSON_PARSE(VAR_MANAGER_IDS, PATH);

# See if we've reached the end of the list
IF VAR_MANAGER_ID IS NULL
THEN 
    BREAK
END;
INSERT INTO LU_MANAGER (MANAGER_ID) VALUES (VAR_MANAGER_ID);
</code></pre>

<p>But there has to be a better way! How can I do something like:</p>

<pre><code>FOREACH JSON_PARSE(VAR_MANAGER_IDS, '$[*]') AS VAR_MANAGER_ID
    INSERT INTO LU_MANAGER (MANAGER_ID) VALUES (VAR_MANAGER_ID);
</code></pre>