Home > Blockchain >  Build a JSON with multiple arrays in Python
Build a JSON with multiple arrays in Python

Time:01-09

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.

Here's my code:

import requests
import json

url = "https://my.api"
payload={}
headers = {
  'Cookie': 'PVEAuthCookie=123'
}

response = requests.request("GET", 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("vmid")
    
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 = { "data": [{"vmid": vmid, "name": name, "type": type, "maxcpu": maxcpu, "maxmem": maxmem, "maxdisk": maxdisk, "cpu": cpu, "mem": mem, "disk": disk, "status": status, "uptime": uptime, "node": node}]}
    
    json_dump = json.dumps(data, indent=2)
    print(json_dump)

json_formatted_str contains the JSON I receive from the API and looks like that:

{
  "data": [
    {
      "status": "running",
      "netin": 44452797,
      "maxdisk": 16729894912,
      "diskwrite": 649285632,
      "node": "pve",
      "uptime": 76654,
      "vmid": 108,
      "id": "lxc/108",
      "type": "lxc",
      "mem": 111636480,
      "cpu": 0.000327262867680765,
      "diskread": 456568832,
      "name": "container108",
      "disk": 2121224192,
      "maxmem": 2147483648,
      "netout": 25054481,
      "maxcpu": 1,
      "template": 0
    },
   more arrays (a lot more)
  ]
}

json_dump looks like that:

{
  "data": [
    {
      "vmid": 108,
      "name": "container108",
      "type": "lxc",
      "maxcpu": 1,
      "maxmem": 2147483648,
      "maxdisk": 16729894912,
      "cpu": 0.0123243844774696,
      "mem": 111116288,
      "disk": 2121342976,
      "status": "running",
      "uptime": 76825,
      "node": "pve"
    }
  ]
}
{
  "data": [
    {
      "vmid": 1007,
    ... more arrays

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.

("data":[ should not be there more then one time at the beginning and the commas at the end of the arrays are missing too.

I want the output to look like this:

{
    "data":[
        {
            "vmid": "100",
            "cpu": "4",
            "mem": "16384" (more keys and values...)
        },
        {
            "vmid": "101",
            "cpu": "2",
            "mem": "4096"
        },
        {
            "vmid": "102",
            "cpu": "6",
            "mem": "32768"
        }
    ]
}

I tried to find examples online and here on Stackoverflow, but I coundn't find anything so I thought I ask here.

CodePudding user response:

You have to append every time new data, not create again. like this

data["data"].append({"vmid": vmid, "name": name, "type": type, "maxcpu": maxcpu, "maxmem": maxmem, "maxdisk": maxdisk, "cpu": cpu, "mem": mem, "disk": disk, "status": status, "uptime": uptime, "node": node})

And than you can dump and print out of the loop.

  •  Tags:  
  • Related