How to obtain specific key values from a JSON response with list of objects and perform arithmetic operation from extracted values

Viewed 33

I have written code to get data from an AWS API. I was able to parse the JSON data from the API. I'm filtering out only the running instances through the API and extract its data. The output is a JSON response.

Im trying to :

  1. retrieve corecount and ThreadsPerCore values of the running instances,
  2. later i will have to multiply corecountx ThreadsPerCore = vcpus of the respective instances.
  3. and summing up all vcpus of the running instances.

How can I write code that does this ?

I read some tutorials, so I understand that I need to use [] to access elements of the nested lists and dictionaries; and I tried retrieving values using it for a single set of data (only for INSTANCE-1) but I can't figure out exactly how it works in my case where I may have 'n' of set of datas ( INSTANCE 1, 2, 3, .....) with specific values needs to be obtained. Hence here I have implemented for loop and again the work gets complex when i had to do the arithmetic operations.

here is the JSON output structure which contains 3 sets of data (for 3 running instances) in this example but might differ based on scenarios:

{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                "AmiLaunchIndex": 0,
                "ImageId": "ami-123",
                "InstanceId": "INSTANCE-1",
                "InstanceType": "t2.micro",
                "KeyName": "MyKeyPair",
                "State": {
                    "Code": 16,
                    "Name": "running"
                },
                "StateTransitionReason": "",
                "CpuOptions": {
                    "CoreCount": 4,
                    "ThreadsPerCore": 2
                },
     ],
        "OwnerId": "123456789012"
        "ReservationId": "r-02a3f596d91211712",
    },
    {
            "Groups": [],
            "Instances": [
                {
                "AmiLaunchIndex": 0,
                "ImageId": "ami-123",
                "InstanceId": "INSATNCE-2",
                "InstanceType": "t2.micro",
                "KeyName": "MyKeyPair",
                "State": {
                    "Code": 16,
                    "Name": "running"
                },
                "StateTransitionReason": "",
                "CpuOptions": {
                    "CoreCount": 8,
                    "ThreadsPerCore": 2
                },
     ],
        "OwnerId": "123456789012"
        "ReservationId": "r-02a3f596d91211712",
    },
    {
            "Groups": [],
            "Instances": [
                {
                "AmiLaunchIndex": 0,
                "ImageId": "ami-123",
                "InstanceId": "INSTANCE-3",
                "InstanceType": "t2.micro",
                "KeyName": "MyKeyPair",
                "State": {
                    "Code": 16,
                    "Name": "running"
                },
                "StateTransitionReason": "",
                "CpuOptions": {
                    "CoreCount": 4,
                    "ThreadsPerCore": 2
                },
     ],
        "OwnerId": "123456789012"
        "ReservationId": "r-02a3f596d91211712",
    }
  ]
}

My code to retrieve CpuOptions : CoreCount and ThreadsPerCore values is :

from shell import run_command_str
import json
cmd = "aws ec2 describe-instances  --output json --filters Name=instance-state-name,Values=running"
output = run_command_str(command=cmd, shell_command=True)
status = json.loads(output)

corecount = {}
for reservation in status1["Reservations"]:
    for instance in reservation["Instances"]:
        corecount[instance["InstanceId"]] = instance["CpuOptions"]["CoreCount"]
print(corecount)

threadspercore = {}

for reservation in status1["Reservations"]:
    for instance in reservation["Instances"]:
        threadspercore[instance["InstanceId"]] = instance["CpuOptions"]["ThreadsPerCore"]
print(threadspercore)

The output for this code:

{'INSTANCE-1': 4, 'INSTANCE-2': 8, 'INSTANCE-3': 4}  ###corecount values
{'INSTANCE-1': 2, 'INSTANCE-2': 2, 'INSTANCE-3': 2}   ###Threadpercore values
1 Answers

If I understand you correctly, you want to compute vcpus and sum of all vcpus from corecount and threadspercore:

corecount = {"INSTANCE-1": 4, "INSTANCE-2": 8, "INSTANCE-3": 4}
threadspercore = {"INSTANCE-1": 2, "INSTANCE-2": 2, "INSTANCE-3": 2}

vcpus = {k: corecount[k] * threadspercore[k] for k in corecount}
sum_vcpus = sum(vcpus.values())

print(corecount)
print(threadspercore)
print(vcpus)
print(sum_vcpus)

Prints:

{'INSTANCE-1': 4, 'INSTANCE-2': 8, 'INSTANCE-3': 4}
{'INSTANCE-1': 2, 'INSTANCE-2': 2, 'INSTANCE-3': 2}
{'INSTANCE-1': 8, 'INSTANCE-2': 16, 'INSTANCE-3': 8}
32
Related