3

I'm trying to extract a specific key and value from json in windows with Python.

I'd like to use the dumps command, but can't find a good example.

**Updated data: An extract from the json file is (but the file is very long):

   {
  "CVE_data_type" : "CVE",
  "CVE_data_format" : "MITRE",
  "CVE_data_version" : "4.0",
  "CVE_data_numberOfCVEs" : "64",
  "CVE_data_timestamp" : "2020-01-09T08:00Z",
  "CVE_Items" : [ {
    "cve" : {
      "data_type" : "CVE",
      "data_format" : "MITRE",
      "data_version" : "4.0",
      "CVE_data_meta" : {
        "ID" : "CVE-2020-0001",
        "ASSIGNER" : "[email protected]"
      },
      "problemtype" : {
        "problemtype_data" : [ {
          "description" : [ ]
        } ]
      },
      "references" : {
        "reference_data" : [ {
          "url" : "https://source.android.com/security/bulletin/2020-01-01",
          "name" : "https://source.android.com/security/bulletin/2020-01-01",
          "refsource" : "CONFIRM",
          "tags" : [ ]
        } ]
      },
      "description" : {
        "description_data" : [ {
          "lang" : "en",
          "value" : "In getProcessRecordLocked of ActivityManagerService.java isolated apps are not handled correctly. This could lead to local escalation of privilege with no additional execution privileges needed. User interaction is not needed for exploitation. Product: Android Versions: Android-8.0, Android-8.1, Android-9, and Android-10 Android ID: A-140055304"
        } ]
      }
    },
    "configurations" : {
      "CVE_data_version" : "4.0",
      "nodes" : [ ]
    },
    "impact" : { },
    "publishedDate" : "2020-01-08T19:15Z",
    "lastModifiedDate" : "2020-01-08T20:01Z"
  }, {
    "cve" : {
      "data_type" : "CVE",
      "data_format" : "MITRE",
      "data_version" : "4.0",
      "CVE_data_meta" : {
        "ID" : "CVE-2020-0002",
        "ASSIGNER" : "[email protected]"
      },
      "problemtype" : {
        "problemtype_data" : [ {
          "description" : [ ]
        } ]
      },
      "references" : {
        "reference_data" : [ {
          "url" : "https://source.android.com/security/bulletin/2020-01-04",
          "name" : "https://source.android.com/security/bulletin/2020-01-04",
          "refsource" : "CONFIRM",
          "tags" : [ ]
        } ]
      },
      "description" : {
        "description_data" : [ {
          "lang" : "en",
          "value" : "In ih264d_init_decoder of ih264d_api.c, there is a possible out of bounds write due to a use after free. This could lead to remote code execution with no additional execution privileges needed. User interaction is needed for exploitation Product: Android Versions: Android-8.0, Android-8.1, Android-9, and Android-10 Android ID: A-142602711"
        } ]
      }
    },
    "configurations" : {
      "CVE_data_version" : "4.0",
      "nodes" : [ ]
    },

    ...

I need to extract the ID and description.

I tried this

for key, value in json.dumps(cve_dict['CVE_Items'][0], sort_keys=True, indent=4, separators=',', ': ')):
    #if(key in ['ID', 'description']):
    print(key, value)

But I get this error:

  File "unzip_get_info.py", line 19
    for key, value in json.dumps(cve_dict['CVE_Items'][0], sort_keys=True, indent=4, separators=',', ': ')):
                                                                                                          ^
SyntaxError: invalid syntax

I can get the whole json to print out with this:

print(json.dumps(cve_dict['CVE_Items'][0], sort_keys=True, indent = 4, separators=(',', ': ')))

I'm not a huge python programmer. Any idea how I can get the key/value of ID and description out? I tried doing it directly with cve_dict, but the error was something about how I can't do that directly.

I'd appreciate the help. This is my first python program.

I'm trying to figure out how to do it with dumps with this link, but I'm not seeing it. I looked at this too, but I'm not sure.

**Update: I tried

for key, value in cve_dict['CVE_Items'][0].items():
    if(key in ['ID', 'description']):
        print(key, value)

trying to limit it to ID and description, in addition to what was suggested below, but it's not printing anything. I know ID and description are in there (see json above). How do I just print ID and description, and not all the key/values?

2 Answers 2

2

You should not convert the dict to a JSON string when the dict itself can be easily traversed. Simply access the desired value of the ID key with:

cve_dict['CVE_Items'][0]['cve']['CVE_data_meta']['ID']

If you want all the IDs you can iterate through the list items with a for loop:

for item in cve_dict['CVE_Items']:
    print(item['cve']['CVE_data_meta']['ID'])
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot! I thought I understood, but when I tried to get description with print(cve_dict['CVE_Items'][0]['cve']['description']['description_data']['value'] ) it's saying TypeError: list indices must be integers or slices, not str
You're welcome. The values of both description and description_data are lists, so you should access the sub-dicts within via indices: cve_dict['CVE_Items'][0]['cve']['description'][0]['description_data'][0]['value']
Idk, it's saying KeyError: 0 with print(cve_dict['CVE_Items'][0]['cve']['description'][0]['description_data'][0]['value'] )
Oops I thought the value of description is a list, which it is not, so: cve_dict['CVE_Items'][0]['cve']['description']['description_data'][0]['value']
Can you update your question with a sample of your data that has more than one cve ID, so that it is clear where the multiple IDs are?
|
0

Could you try iterate over dict:

for k, v in cve_dict['CVE_Items'][0].items():
    print(k, v)

The json.dumps is to convert dict back to string, not python object to iterate over,

2 Comments

it's printing the info, but how do I limit it to the KEY/value of ID and description?
I added **Update to the question. When I do what you say, it prints everything, not just ID and description. When I try to add the if, it prints nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.