-2

There is a json file that has data I am looking to throw into a database, but there are over 10000 entries. So I want to automate it.

Here is an example of the parsed json code through python:

{'product': {'product_data': [{'product_name': 'debian_linux',
                                                                                             'version': {'version_data': [{'version_value': '8.0'}]}}]},
                                                            'vendor_name': 'debian'}]}},
                    'data_format': 'MITRE',
                    'data_type': 'CVE',
                    'data_version': '4.0',

basically where it says 'product_name': 'debian_linux' is what I'm after. I want to print out each product name in the json file, but only show 1 of the product_name if it has more than one entry (ex: there could be like 15 mentions of debian_linux, or windows_server_2008, but I only want to see one of those fifteen.)

I read the documentation for the jsons library, but it's hard to understand without having much experience with coding, but I gave it a shot and here is my code so far:

import json
from pprint import pprint

with open('nvd.json') as f:
    data = json.load(f)

pprint(data["product_name"])

I get an error saying KeyError: 'product_name'

Nick L
  • 11
  • 3
  • See e.g. https://docs.python.org/3/tutorial/datastructures.html#dictionaries; you need to know basic data structures. – jonrsharpe Jun 20 '18 at 12:01
  • @jonrsharpe I know what data structures are. – Nick L Jun 20 '18 at 12:11
  • Then why are you trying to access a key that clearly isn't in the dictionary you're trying to access it in? – jonrsharpe Jun 20 '18 at 12:12
  • I understand that these are nested dictionaries, or possibly also a list within a dict, but I am trying to figure out how to access it. – Nick L Jun 20 '18 at 12:16

2 Answers2

0

As mentioned in the comments, you have nested dictionaries, ie. you got to loop over them with e.g. for key, values in data.items() and then verify that the key corresponds to your product_name.

see also iterating over dictionaries

cuda12
  • 600
  • 3
  • 15
0

I've figured it out!

import json
import sqlite3

with open('nvd.json', 'r') as file:
    parsed = json.load(file)

for item in parsed['CVE_Items']:
    for x in item:
        for test in item['cve']['affects']['vendor']['vendor_data']:

            print(test['product']['product_data'][0]['product_name'])
Nick L
  • 11
  • 3