问题
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" : "cve@mitre.org"
},
"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" : "cve@mitre.org"
},
"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?
回答1:
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'])
回答2:
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,
来源:https://stackoverflow.com/questions/59656343/python-extracting-specific-key-and-value-from-json-not-working