Removing items from JSON using Python loop

Question:

how do I iterate over the data and keep object keys that have the string "Java" in the value and remove keys with the string "Javascript" in the value? In addition to the iterations I already have in my code. For example:

this key has the word ‘Java’ in the value.

          "value" : "A vulnerability in the encryption implementation of EBICS messages in the open source librairy ebics-java/ebics-java-client allows an attacker sniffing network traffic to decrypt EBICS payloads. This issue affects: ebics-java/ebics-java-client versions prior to 1.2."

the current code below iterates thru other JSON items (that are also needed), but not the Java/Javascript issue.

from encodings import utf_8
import json
from zipfile import ZipFile
from urllib.request import urlretrieve
from io import BytesIO
import os


url = "https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-2022.json.zip"
urlretrieve(url, "nvdcve-1.1-2022.json.zip")

with ZipFile('nvdcve-1.1-2022.json.zip', 'r') as zip:
    zip.extractall('.')

with open('nvdcve-1.1-2022.json', encoding='utf-8') as x:
    data = json.load(x)

#function to sort through without rewriting code with parameters/arguments passed into the function(variable)  
def base_score(metric):
    if 'baseMetricV3' not in metric['impact']:
        #no values = 0 so it will auto sort by ID
        return (0, metric['cve']['CVE_data_meta']['ID'])
        #sorts by ID if two or more base scores are equal
    return (metric['impact']['baseMetricV3']['cvssV3']['baseScore'], metric['cve']['CVE_data_meta']['ID'])
    #return allows assigment of function output to new variable

#direct python to open json file using specific encoding to avoid encoding error

for CVE_Item in data['CVE_Items']:
    for node in CVE_Item['configurations']['nodes']:
        #removes items while iterating through them
        node['cpe_match'][:] = [item for item in node['cpe_match'] if item['vulnerable']]
        #also check children objects for vulnerable 
        if node['children']:
            for children_node in node['children']:
                children_node['cpe_match'][:] = [item for item in children_node['cpe_match'] if item['vulnerable']]

#sorts data in descending order using reverse
data['CVE_Items'].sort(reverse=True, key=base_score)

#write file to current working directory
with open('sorted_nvdcve-1.1-2022.json', 'w') as new_file:
    new_file.write(json.dumps(data, indent=4))

if os.path.exists('nvdcve-1.1-2022.json.zip'):
  os.remove('nvdcve-1.1-2022.json.zip')
else:
  print("The file does not exist") 

if os.path.exists('nvdcve-1.1-2022.json'):
  os.remove('nvdcve-1.1-2022.json')
else:
  print("The file does not exist") 


   


here is the link to the original JSON file (too large to post entire text here):
https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-2022.json.zip

the key ‘value’ is located in the ‘description’ list.

here is a sample of the JSON text:

{
  "CVE_data_type" : "CVE",
  "CVE_data_format" : "MITRE",
  "CVE_data_version" : "4.0",
  "CVE_data_numberOfCVEs" : "15972",
  "CVE_data_timestamp" : "2022-11-01T07:00Z",
  "CVE_Items" : [ {
    "cve" : {
      "data_type" : "CVE",
      "data_format" : "MITRE",
      "data_version" : "4.0",
      "CVE_data_meta" : {
        "ID" : "CVE-2022-0001",
        "ASSIGNER" : "[email protected]"
      },
      "problemtype" : {
        "problemtype_data" : [ {
          "description" : [ {
            "lang" : "en",
            "value" : "NVD-CWE-noinfo"
          } ]
        } ]
      },
      "references" : {
        "reference_data" : [ {
          "url" : "https://www.intel.com/content/www/us/en/security-center/advisory/intel-sa-00598.html",
          "name" : "https://www.intel.com/content/www/us/en/security-center/advisory/intel-sa-00598.html",
          "refsource" : "MISC",
          "tags" : [ "Vendor Advisory" ]
        }, {
          "url" : "http://www.openwall.com/lists/oss-security/2022/03/18/2",
          "name" : "[oss-security] 20220318 Xen Security Advisory 398 v2 - Multiple speculative security issues",
          "refsource" : "MLIST",
          "tags" : [ "Mailing List", "Third Party Advisory" ]
        }, {
          "url" : "https://www.oracle.com/security-alerts/cpujul2022.html",
          "name" : "N/A",
          "refsource" : "N/A",
          "tags" : [ "Patch", "Third Party Advisory" ]
        }, {
          "url" : "https://security.netapp.com/advisory/ntap-20220818-0004/",
          "name" : "https://security.netapp.com/advisory/ntap-20220818-0004/",
          "refsource" : "CONFIRM",
          "tags" : [ "Third Party Advisory" ]
        } ]
      },
      "description" : {
        "description_data" : [ {
          "lang" : "en",
          "value" : "JavaScript sharing of branch predictor selectors between contexts in some Intel(R) Processors may allow an authorized user to potentially enable information disclosure via local access."
        } ]
      }
Asked By: paliknight

||

Answers:

Add this inside the for CVE_Item loop.

CVE_Item['cve']['description']['description_data'] = [
    d for d in CVE_Item['cve']['description']['description_data'] 
    if 'Java' in d['value'] and 'JavaScript' not in d['value']]

The modified loop looks like:

for CVE_Item in data['CVE_Items']:
    CVE_Item['cve']['description']['description_data'] = [
        d for d in CVE_Item['cve']['description']['description_data'] 
        if 'Java' in d['value'] and 'JavaScript' not in d['value']]
    for node in CVE_Item['configurations']['nodes']:
        #removes items while iterating through them
        node['cpe_match'][:] = [item for item in node['cpe_match'] if item['vulnerable']]
        #also check children objects for vulnerable 
        if node['children']:
            for children_node in node['children']:
                children_node['cpe_match'][:] = [item for item in children_node['cpe_match'] if item['vulnerable']]
Answered By: Barmar
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.