leo Stahl
leo Stahl

Reputation: 100

How to read data out of a JSON array

I want to read data out of a JSON array and put them into variables. The only way worked for me was replacing the }, { with }, and the [ and ] with nothing and the reading it like it was a normal json file. How can i do that? This is the Json File

    {
  "Area": "Hyper-V Cluster",
  "time_start": "2020-03-07T00:00:00",
  "time_end": "2020-03-07T23:59:59",
  "Systeme": [
    {
      "name": "test-s-hc001",
      "CPU [%]": 20.86,
      "Disk [%]": 4.32
    },
    {
      "name": "test-s-hc002",
      "CPU  [%]": 21.23,
      "Disk [%]": 49.42
    },
    {
      "name": "test-s-hc003",
      "CPU  [%]": 19.8,
      "Disk [%]": 7.47
    },
    {
      "name": "test-s-hc004",
      "CPU [%]": 20.01,
      "Disk [%]": 49.49
    },
    {
      "name": "test-s-hc005",
      "CPU [%]": 21.1,
      "Disk [%]": 48.41
    },
    {
      "name": "test-s-hc006",
      "CPU [%]": 21.01,
      "Disk [%]": 53.71
    },
    {
      "name": "test-s-hc007",
      "CPU [%]": 3.75,
      "Disk [%]": 62.72
    },
    {
      "name": "test-s-hc009",
      "CPU [%]": 4.11,
      "Disk [%]": 59.66
    }
  ]
}

My Code i actually tried but doesn not working looks like this:

import json

input_file = open ('test.json')
json_array = json.load(input_file)
windows_list = []

for item in json_array:
    windows_details = {"name":None, "Cpu [%]":None, "Disk [%]":None}
    windows_details['name'] = item['name']
    windows_details['CPU [%]'] = item['CPU [%]']
    windows_details['Disk[%]'] = item['Disk']
    windows_list.append(windows_details)

print(windows_list)

The output should look like this

test-s-hc001
20.86
4.32
test-s-hc002

...

Upvotes: 0

Views: 100

Answers (3)

mspiller
mspiller

Reputation: 3839

Your json_array is called Systeme in the json struct and not the top element.

So you should use that:

import json

input_file = open ('test.json')
json_array = json.load(input_file)
windows_list = []

for item in json_array['Systeme']:
    windows_details = {"name":None, "CPU [%]":None, "Disk [%]":None}
    windows_details['name'] = item['name']
    windows_details['CPU [%]'] = item['CPU [%]']
    windows_details['Disk [%]'] = item['Disk [%]']
    windows_list.append(windows_details)

print(windows_list)

Upvotes: 4

user12758604
user12758604

Reputation: 375

Since the data you want is in 'Systeme' list, you can access each item by the following method:

for i in json_array['Systeme']:
    print(i['name'])
    print(i['CPU [%]'])
    print(i['Disk [%]'])

I think my answer echos what Miller have shared, but if you would like to get the output you shared, then this is the way to go. Besides, I don't really quite understand why would you append it back to a list, when d['Systeme'] itself is almost identical to windows_list

Upvotes: 1

damaredayo
damaredayo

Reputation: 1079

You can do this:

import json
with open("file.json", "r") as i:
    jsonObject = json.loads(i.read())

Then reference jsonObject as a dictionary like you normally would

Upvotes: -1

Related Questions