title: Build a JSON with multiple arrays in Python
body: <p>im calling an API in Python and get a JSON response. Im filtering that response for the values I need. Then I want to make a JSON from that values again and print it.</p>
  <p>Here's my code:</p>
  <pre><code>import requests
  import json
  
  url = &quot;https://my.api&quot;
  payload={}
  headers = {
    'Cookie': 'PVEAuthCookie=123'
  }
  
  response = requests.request(&quot;GET&quot;, url, headers=headers, data=payload)
      
  json_object = json.loads(response.text)
  json_formatted_str = json.dumps(json_object, indent=2)
      
  vmid_count = json_formatted_str.count(&quot;vmid&quot;)
      
  i = 0
      
  for i in range(vmid_count):
      vmid = json_object['data'][i]['vmid']
      cpu = json_object['data'][i]['cpu']
      mem = json_object['data'][i]['mem']
      
      data = { &quot;data&quot;: [{&quot;vmid&quot;: vmid, &quot;name&quot;: name, &quot;type&quot;: type, &quot;maxcpu&quot;: maxcpu, &quot;maxmem&quot;: maxmem, &quot;maxdisk&quot;: maxdisk, &quot;cpu&quot;: cpu, &quot;mem&quot;: mem, &quot;disk&quot;: disk, &quot;status&quot;: status, &quot;uptime&quot;: uptime, &quot;node&quot;: node}]}
      
      json_dump = json.dumps(data, indent=2)
      print(json_dump)
  </code></pre>
  <p>json_formatted_str contains the JSON I receive from the API and looks like that:</p>
  <pre><code>{
    &quot;data&quot;: [
      {
        &quot;status&quot;: &quot;running&quot;,
        &quot;netin&quot;: 44452797,
        &quot;maxdisk&quot;: 16729894912,
        &quot;diskwrite&quot;: 649285632,
        &quot;node&quot;: &quot;pve&quot;,
        &quot;uptime&quot;: 76654,
        &quot;vmid&quot;: 108,
        &quot;id&quot;: &quot;lxc/108&quot;,
        &quot;type&quot;: &quot;lxc&quot;,
        &quot;mem&quot;: 111636480,
        &quot;cpu&quot;: 0.000327262867680765,
        &quot;diskread&quot;: 456568832,
        &quot;name&quot;: &quot;container108&quot;,
        &quot;disk&quot;: 2121224192,
        &quot;maxmem&quot;: 2147483648,
        &quot;netout&quot;: 25054481,
        &quot;maxcpu&quot;: 1,
        &quot;template&quot;: 0
      },
     more arrays (a lot more)
    ]
  }
  </code></pre>
  <p>json_dump looks like that:</p>
  <pre><code>{
    &quot;data&quot;: [
      {
        &quot;vmid&quot;: 108,
        &quot;name&quot;: &quot;container108&quot;,
        &quot;type&quot;: &quot;lxc&quot;,
        &quot;maxcpu&quot;: 1,
        &quot;maxmem&quot;: 2147483648,
        &quot;maxdisk&quot;: 16729894912,
        &quot;cpu&quot;: 0.0123243844774696,
        &quot;mem&quot;: 111116288,
        &quot;disk&quot;: 2121342976,
        &quot;status&quot;: &quot;running&quot;,
        &quot;uptime&quot;: 76825,
        &quot;node&quot;: &quot;pve&quot;
      }
    ]
  }
  {
    &quot;data&quot;: [
      {
        &quot;vmid&quot;: 1007,
      ... more arrays
  </code></pre>
  <p>It starts a whole new object every time it runs through the for-loop. If I remove the print(json_dump) from the loop, I only get the last array.</p>
  <p>(&quot;data&quot;:[ should not be there more then one time at the beginning and the commas at the end of the arrays are missing too.</p>
  <p>I want the output to look like this:</p>
  <pre><code>{
      &quot;data&quot;:[
          {
              &quot;vmid&quot;: &quot;100&quot;,
              &quot;cpu&quot;: &quot;4&quot;,
              &quot;mem&quot;: &quot;16384&quot; (more keys and values...)
          },
          {
              &quot;vmid&quot;: &quot;101&quot;,
              &quot;cpu&quot;: &quot;2&quot;,
              &quot;mem&quot;: &quot;4096&quot;
          },
          {
              &quot;vmid&quot;: &quot;102&quot;,
              &quot;cpu&quot;: &quot;6&quot;,
              &quot;mem&quot;: &quot;32768&quot;
          }
      ]
  }
  </code></pre>
  <p>I tried to find examples online and here on Stackoverflow, but I coundn't find anything so I thought I ask here.</p>