Search code examples
jsonpython-3.xlistdictionaryindexoutofrangeexception

PYTHON3 | Parsing JSON | index out of range | can't access to 1st column of an array


I have this JSON (I don't give you the whole thing because it's freaking long but you don't need the rest.)

cve" : {
 "data_type" : "CVE",
 "data_format" : "MITRE",
 "data_version" : "4.0",
 "CVE_data_meta" : {
   "ID" : "CVE-2018-9991",
   "ASSIGNER" : "[email protected]"
 },
 "affects" : {
   "vendor" : {
     "vendor_data" : [ {
       "vendor_name" : "frog_cms_project",
       "product" : {
         "product_data" : [ {
           "product_name" : "frog_cms",
           "version" : {
             "version_data" : [ {
               "version_value" : "0.9.5"
             } ]
           }
         } ]
       }
     } ]
   }
 },

What I want to do is to print the vendor name of this cve. So, what I did is :

with open("nvdcve-1.0-2018.json", "r") as file:
    data = json.load(file)
    increment = 0
    number_cve = data["CVE_data_numberOfCVEs"]
    while increment < int(number_cve):
        print (data['CVE_Items'][increment]['cve']['CVE_data_meta']['ID'])
        print (',')
        print (data['CVE_Items'][increment]['cve']['affects']['vendor']['vendor_data'][0]['vendor_name'])        
        print ("\n")
        increment += 

The reason I did a while is because in the JSON file, there is a lot of CVEs, this is why I did data['CVE_Items'][increment]['cve'] (and this part works fine, the line `print (data['CVE_Items'][increment]['cve']['CVE_data_meta']['ID'] is working well).

My error is in the print (data['CVE_Items'][increment]['cve']['affects']['vendor']['vendor_data'][0]['vendor_name']) line, python returns a list index out of range error.

But if I'm reading this JSON well, vendor_data is an array of 1 column so vendor_name is the ['vendor_data'][0]['vendor_name'] isn't it ?

The only way to parse the vendor_name i found is :

for value in data['CVE_Items'][a]['cve']['affects']['vendor']['vendor_data']:
    print (value['vendor_name'])

instead of print (data['CVE_Items'][increment]['cve']['affects']['vendor']['vendor_data'][0]['vendor_name'])

And doing a for just for one iteration is pretty disgusting :s, but at least, value is the data['CVE_Items'][a]['cve']['affects']['vendor']['vendor_data'][0] that I wanted....

Anyone knows something about it ?


Solution

  • Make sure every CVE_Item has an vender_data.

    Example:

    with open("nvdcve-1.0-2018.json", "r") as file:
        data = json.load(file)
        increment = 0
        number_cve = data["CVE_data_numberOfCVEs"]
        while increment < int(number_cve):
            print (data['CVE_Items'][increment]['cve']['CVE_data_meta']['ID'])
            print (',')
            if (len(data['CVE_Items'][increment]['cve']['affects']['vendor']['vendor_data']) > 0) :
                print (data['CVE_Items'][increment]['cve']['affects']['vendor']['vendor_data'][0]['vendor_name'])        
            print ("\n")
            increment +=