Search code examples
pythonjsonapigetnested

Printing Nested Json API data using Python


I was able to receive the data using an API get request, now I just need help printing a few objects. I'm having trouble because the objects I need are nested pretty deeply. Objects I need:

-cve ID -url ref -description -severity

json page: https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/

import requests
import json
import pprint

url = "https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/"
params = {"q": "CVE"}
response = requests.get(url, params)

data = json.loads(response.text)


pprint.pprint (data)

Solution

  • import requests
    import json
    import pprint
    
    url = "https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/"
    params = {"q": "CVE"}
    
    response = requests.get(url, params)
    
    data = json.loads(response.content)    
    
    pprint.pprint(data)
    

    response.content will return the content of the response. after than:

    cve ID: pprint.pprint(data['result']['CVE_Items'][0]['cve']['CVE_data_meta']['ID'])

    url ref: pprint.pprint(data['result']['CVE_Items'][0]['cve']['references']['reference_data'][0]['url'])

    description: pprint.pprint(data['result']['CVE_Items'][0]['cve']['description']['description_data'][0]['value'])

    severity: pprint.pprint(data['result']['CVE_Items'][0]['impact']['baseMetricV2']['severity'])