How to access a value of a dictionary inside a dictionary within a list using python

Viewed 27

I have the following json object that I am trying to extract some value - name field under the items list.

{
    "apiVersion":"aquasecurity.github.io/v1alpha1",
    "items":[
       {
          "apiVersion":"aquasecurity.github.io/v1alpha1",
          "kind":"VulnerabilityReport",
          "metadata":{
             "annotations":{
                "trivy-operator.aquasecurity.github.io/report-ttl":"730h0m0s"
             },
             "name":"cronjob-eks-upgrade-notifier-eks-upgrade-notifier",
             "namespace":"devops",             
             "resourceVersion":"174823549",
             "uid":"c93d513a-a940-4c5c-a8ec-f83a9b67b2cd"
          },
          "report":{
             "artifact":{
                "repository":"myrepo/k8s-eks-upgrade-agent",
                "tag":"9478954"
             },
             "registry":{
                "server":"<account-id>.dkr.ecr.eu-central-1.amazonaws.com"
             },
             "scanner":{
                "name":"Trivy",
                "vendor":"Aqua Security",
                "version":"0.30.0"
             },
             "summary":{
                "criticalCount":1,
                "highCount":4,
                "lowCount":0,
                "mediumCount":0,
                "noneCount":0,
                "unknownCount":0
             },
             "updateTimestamp":"2022-09-05T15:02:24Z",
             "vulnerabilities":[
                {
                   "fixedVersion":"1.35.0-r15",
                   "installedVersion":"1.35.0-r13",
                   "links":[
                      
                   ],
                   "primaryLink":"https://avd.aquasec.com/nvd/cve-2022-30065",
                   "resource":"busybox",
                   "score":6.2,
                   "severity":"HIGH",
                   "target":"",
                   "title":"busybox: A use-after-free in Busybox's awk applet leads to denial of service",
                   "vulnerabilityID":"CVE-2022-30065"
                }                
             ]
          }
       },
       {
          "apiVersion":"aquasecurity.github.io/v1alpha1",
          "kind":"VulnerabilityReport",
          "metadata":{
             "annotations":{
                "trivy-operator.aquasecurity.github.io/report-ttl":"730h0m0s"
             },
             "name":"replicaset-bg-cm-operator-b9f798f4-bg-cm-operator",
             "namespace":"devops",             
             "resourceVersion":"175193254",
             "uid":"b9c9830e-95f0-45e9-871c-d0ccef97abcb"
          },
          "report":{
             "artifact":{
                "repository":"myrepo7/k8s-blue-green-cm-operator",
                "tag":"5c7cbca"
             },
             "registry":{
                "server":"<account-id>.dkr.ecr.eu-central-1.amazonaws.com"
             },
             "scanner":{
                "name":"Trivy",
                "vendor":"Aqua Security",
                "version":"0.30.0"
             },
             "summary":{
                "criticalCount":1,
                "highCount":5,
                "lowCount":0,
                "mediumCount":0,
                "noneCount":0,
                "unknownCount":0
             },
             "updateTimestamp":"2022-09-06T06:26:24Z",
             "vulnerabilities":[
                {
                   "fixedVersion":"1.3.2-r1",
                   "installedVersion":"1.3.2-r0",
                   "links":[
                      
                   ],
                   "primaryLink":"https://avd.aquasec.com/nvd/cve-2021-46828",
                   "resource":"libtirpc",
                   "score":7.5,
                   "severity":"HIGH",
                   "target":"",
                   "title":"libtirpc: DoS vulnerability with lots of connections",
                   "vulnerabilityID":"CVE-2021-46828"
                }                
             ]
          }
       }
    ],
    "kind":"VulnerabilityReportList",
    "metadata":{
       "continue":"",
       "resourceVersion":"184454555"
    }
 }

I tried the following code but it gives me the output as single letters:

def get_all_vulnerability_reports():
    logging.info("Getting all the \"vuln\" type objects in * namespaces")
    connect_to_k8s_cluster()
    vuln_reports = set()

    with k8s_client.ApiClient() as api_client:
        api_instance = k8s_client.CustomObjectsApi(api_client)
        try:
            # vuln_list = api_instance.list_cluster_custom_object(group="aquasecurity.github.io", version="v1alpha1", plural="vulnerabilityreports", )
            vuln_list = api_instance.list_namespaced_custom_object(group="aquasecurity.github.io", version="v1alpha1", plural="vulnerabilityreports", pretty="true", namespace="devops" )
            
            # print(json.dumps(vuln_list))
        except ApiException as e:
            logging.exception("Exception when trying to get list of  \"vuln\" reports", exc_info=e)
        else:

            for items in json.dumps(vuln_list):
                for item in items:
                    print (item)
        
    return vuln_reports

Can someone help me to fix this?

I need the items.name value.

0 Answers
Related