title: How to obtain specific key values from a JSON response with list of objects and perform arithmetic operation from extracted values
body: <p>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.</p>
  <p>Im trying to :</p>
  <ol>
  <li>retrieve <code>corecount</code> and <code>ThreadsPerCore</code> values of the running instances,</li>
  <li>later i will have to multiply <code>corecount</code>x <code>ThreadsPerCore</code> = <code>vcpus</code> of the respective instances.</li>
  <li>and summing up all <code>vcpus</code> of the running instances.</li>
  </ol>
  <p>How can I write code that does this ?</p>
  <p>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 <code>for loop</code> and again the work gets complex when i had to do the arithmetic operations.</p>
  <p>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:</p>
  <pre><code>{
      &quot;Reservations&quot;: [
          {
              &quot;Groups&quot;: [],
              &quot;Instances&quot;: [
                  {
                  &quot;AmiLaunchIndex&quot;: 0,
                  &quot;ImageId&quot;: &quot;ami-123&quot;,
                  &quot;InstanceId&quot;: &quot;INSTANCE-1&quot;,
                  &quot;InstanceType&quot;: &quot;t2.micro&quot;,
                  &quot;KeyName&quot;: &quot;MyKeyPair&quot;,
                  &quot;State&quot;: {
                      &quot;Code&quot;: 16,
                      &quot;Name&quot;: &quot;running&quot;
                  },
                  &quot;StateTransitionReason&quot;: &quot;&quot;,
                  &quot;CpuOptions&quot;: {
                      &quot;CoreCount&quot;: 4,
                      &quot;ThreadsPerCore&quot;: 2
                  },
       ],
          &quot;OwnerId&quot;: &quot;123456789012&quot;
          &quot;ReservationId&quot;: &quot;r-02a3f596d91211712&quot;,
      },
      {
              &quot;Groups&quot;: [],
              &quot;Instances&quot;: [
                  {
                  &quot;AmiLaunchIndex&quot;: 0,
                  &quot;ImageId&quot;: &quot;ami-123&quot;,
                  &quot;InstanceId&quot;: &quot;INSATNCE-2&quot;,
                  &quot;InstanceType&quot;: &quot;t2.micro&quot;,
                  &quot;KeyName&quot;: &quot;MyKeyPair&quot;,
                  &quot;State&quot;: {
                      &quot;Code&quot;: 16,
                      &quot;Name&quot;: &quot;running&quot;
                  },
                  &quot;StateTransitionReason&quot;: &quot;&quot;,
                  &quot;CpuOptions&quot;: {
                      &quot;CoreCount&quot;: 8,
                      &quot;ThreadsPerCore&quot;: 2
                  },
       ],
          &quot;OwnerId&quot;: &quot;123456789012&quot;
          &quot;ReservationId&quot;: &quot;r-02a3f596d91211712&quot;,
      },
      {
              &quot;Groups&quot;: [],
              &quot;Instances&quot;: [
                  {
                  &quot;AmiLaunchIndex&quot;: 0,
                  &quot;ImageId&quot;: &quot;ami-123&quot;,
                  &quot;InstanceId&quot;: &quot;INSTANCE-3&quot;,
                  &quot;InstanceType&quot;: &quot;t2.micro&quot;,
                  &quot;KeyName&quot;: &quot;MyKeyPair&quot;,
                  &quot;State&quot;: {
                      &quot;Code&quot;: 16,
                      &quot;Name&quot;: &quot;running&quot;
                  },
                  &quot;StateTransitionReason&quot;: &quot;&quot;,
                  &quot;CpuOptions&quot;: {
                      &quot;CoreCount&quot;: 4,
                      &quot;ThreadsPerCore&quot;: 2
                  },
       ],
          &quot;OwnerId&quot;: &quot;123456789012&quot;
          &quot;ReservationId&quot;: &quot;r-02a3f596d91211712&quot;,
      }
    ]
  }
  </code></pre>
  <p>My code to retrieve <code>CpuOptions</code> :  <code>CoreCount</code> and <code>ThreadsPerCore</code> values is :</p>
  <pre><code>from shell import run_command_str
  import json
  cmd = &quot;aws ec2 describe-instances  --output json --filters Name=instance-state-name,Values=running&quot;
  output = run_command_str(command=cmd, shell_command=True)
  status = json.loads(output)
  
  corecount = {}
  for reservation in status1[&quot;Reservations&quot;]:
      for instance in reservation[&quot;Instances&quot;]:
          corecount[instance[&quot;InstanceId&quot;]] = instance[&quot;CpuOptions&quot;][&quot;CoreCount&quot;]
  print(corecount)
  
  threadspercore = {}
  
  for reservation in status1[&quot;Reservations&quot;]:
      for instance in reservation[&quot;Instances&quot;]:
          threadspercore[instance[&quot;InstanceId&quot;]] = instance[&quot;CpuOptions&quot;][&quot;ThreadsPerCore&quot;]
  print(threadspercore)
  </code></pre>
  <p>The output for this code:</p>
  <pre><code>{'INSTANCE-1': 4, 'INSTANCE-2': 8, 'INSTANCE-3': 4}  ###corecount values
  {'INSTANCE-1': 2, 'INSTANCE-2': 2, 'INSTANCE-3': 2}   ###Threadpercore values
  </code></pre>