title: Transform JSON with Groovy
body: <p>I have a JSON file:</p>
  <pre><code>def jsonTxt =
  '''
  {
      &quot;fields&quot;: [
         &quot;key1&quot;,
         &quot;key2&quot;,
         &quot;key3&quot;
      ],
      &quot;table&quot;: [
        [
          1,
          2,
          &quot;three&quot;
        ],
        [
         4,
         5,
         &quot;six&quot;
        ],
        [
         7,
         8,
         &quot;nine&quot;
        ]
      ] 
  }     
  '''     
  </code></pre>
  <p>Values in <code>fields</code> array are names (keys) for values in <code>table</code> array. I want to match them like this:</p>
  <pre><code>[
      {
          &quot;key1&quot;: 1,
          &quot;key2&quot;: 2,
          &quot;key3&quot;: &quot;three&quot;
      },
      {
          &quot;key1&quot;: 4,
          &quot;key2&quot;: 5,
          &quot;key3&quot;: &quot;six&quot;
      },
      {
          &quot;key1&quot;: 7,
          &quot;key2&quot;: 8,
          &quot;key3&quot;: &quot;nine&quot;
      }
  ]
  </code></pre>
  <p>I tried with:</p>
  <pre><code>def result =  jsonSlurper.parseText(jsonTxt)
  
  def myRes = result.table.collect {
       [
         key1 : it[0],
         key2 : it[1],
         key3 : it[2]
       ]  
  }
  </code></pre>
  <p>And it returns what i expected. But how to avoid this hardcode? I mean, json property name I should take from <code>fields</code> and values for them like with increment indexes. If in the next JSON there will be another field, e.g. <code>accountId</code> instead of <code>key1</code> or as additional field, my code will be not working correctly.</p>