title: Accessing an array object from JSON by index-variable
body: <p>I want to access over an array object in JSON by index with a variable. Consider the following code:</p>
  <pre><code>declare @pjson nvarchar(max)='{
      &quot;store&quot;:{
          &quot;storeId&quot;: 100,
          &quot;name&quot;: &quot;TEST&quot;,
          &quot;lastUpdatedBy&quot;: &quot;MULE&quot;,
          &quot;location&quot;: {
          declare @pjson nvarchar(max)='{
      &quot;store&quot;:{
          &quot;storeId&quot;: 100,
          &quot;name&quot;: &quot;TEST&quot;,
          &quot;lastUpdatedBy&quot;: &quot;MULE&quot;,
          &quot;location&quot;: {
              &quot;addresses&quot;: [
                  {
                      &quot;addressType&quot;: &quot;MAIN&quot;,
                      &quot;name&quot;: &quot;Name1&quot;,
                      &quot;name2&quot;: &quot;Name2&quot;,
                      &quot;address&quot;: &quot;Address1&quot;,
                      &quot;address2&quot;: &quot;Address2&quot;,
                      &quot;city&quot;: &quot;City&quot;,
                      &quot;lastUpdateBy&quot;: &quot;MULE&quot;
                  },
                  {
                      &quot;addressType&quot;: &quot;SECONDARY&quot;,
                      &quot;name&quot;: &quot;Name1&quot;,
                      &quot;name2&quot;: &quot;Name2&quot;,
                      &quot;address&quot;: &quot;Address1&quot;,
                      &quot;address2&quot;: &quot;Address2&quot;,
                      &quot;city&quot;: &quot;City&quot;,
                      &quot;lastUpdateBy&quot;: &quot;MULE&quot;
                  },
                  {
                      &quot;addressType&quot;: &quot;BILLING&quot;,
                      &quot;name&quot;: &quot;Name1&quot;,
                      &quot;name2&quot;: &quot;Name2&quot;,
                      &quot;address&quot;: &quot;Address1&quot;,
                      &quot;address2&quot;: &quot;Address2&quot;,
                      &quot;city&quot;: &quot;City&quot;,
                      &quot;lastUpdateBy&quot;: &quot;MULE&quot;
                  }
              ]
          }
      }
  }'
  
  Declare @counter1 INT = 0;
  Print JSON_VALUE(@pjson,N'lax $.store.location.addresses[@counter1].addressType')
  </code></pre>
  <p>I get an error:</p>
  <blockquote>
  <p>JSON path is not properly formatted. Unexpected character '@' is found
  at position 31.</p>
  </blockquote>
  <p>If I try directly by passing number as</p>
  <pre><code>Declare @counter1 INT = 0;
  Print JSON_VALUE(@pjson,N'lax $.store.location.addresses[0].addressType')
  </code></pre>
  <p>I get the expected result</p>
  <pre><code>MAIN
  </code></pre>
  <p>Is there something that I am missing while passing the variable?</p>