title: SQL Server 2016: can't reference JSON array path using JSON_MODIFY
body: <p>I have tried for a few hours to get my issue working, but I am unsuccessful.</p>
  
  <p>I pass a complex JSON object to a stored procedure and after I insert the main record, I then get that new record ID, and use it to change a couple other ID's within the JSON object.</p>
  
  <p>I am successful using JSON_MODIFY like this.. when the value is not within an array.</p>
  
  <pre><code>SET @json = JSON_MODIFY(@json, '$.Weather.RoundID', @round_identity) 
  </code></pre>
  
  <p>but when the value is within an array, I can't seem to get it to work.</p>
  
  <p>This works when I hardcode the index of the path:</p>
  
  <pre><code>SET @json = JSON_MODIFY(@json, '$.Scores[0].RoundID', @round_identity);
  </code></pre>
  
  <p>This doesn't work:</p>
  
  <pre><code>SET @json = JSON_MODIFY(@json, '$.Scores['+ STR(@Score_Counter) + '].RoundID', @round_identity);
  </code></pre>
  
  <p>The error is: </p>
  
  <blockquote>
    <p>The argument 2 of the "JSON_MODIFY" must be a string literal</p>
  </blockquote>
  
  <p>The value <code>@Score_Counter</code> is generated by a while loop to index the current array object.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thank you</p>
  
  <p>Bill</p>
  
  <p>Update:
  With the help of Bogdan, I was able to successfully change the json in the arrays.
  Here is what I am using:</p>
  
  <pre><code>--Loop and populate all scores to set RoundID using Dynamic SQL
  Set @Score_Counter = 0;
  Set @Score_Count = (SELECT COUNT(*) FROM OPENJSON(@json, N'lax $.Scores'))
  
  WHILE @Score_Counter &lt; @Score_Count
  BEGIN
      SELECT  @SqlStatement = CONCAT(N'SELECT @json = JSON_MODIFY    (@json, ''$.Scores[', @Score_Counter, N'].RoundID'', ' + cast(@round_identity as nvarchar(10))+')')
      EXEC    sp_executesql @SqlStatement, N'@json NVARCHAR(MAX) OUTPUT', @json = @json OUTPUT
      SET @Score_Counter = @Score_Counter + 1;
  END;
  </code></pre>