[英]Python Getting Specific JSON values from AWS GuardDuty
I've written some code on AWS Lambda and I am trying to extract the IPs from GuardDuty's findings. 我已经在AWS Lambda上编写了一些代码,并且试图从GuardDuty的发现中提取IP。 I have gotten the FindingIds okay, but when I try and extract the IP address I get the following error: 我已经可以使用FindingIds,但是当我尝试提取IP地址时,出现以下错误:
{ "errorMessage": "list indices must be integers or slices, not str", "errorType": "TypeError", "stackTrace": [ [ "/var/task/lambda_function.py", 38, "lambda_handler", "print(loadFindings['Findings']['Resource']['NetworkInterfaces']['PublicIp'])" ] ] } {“” ErrorMessage“:”列表索引必须是整数或切片,而不是str“,” errorType“:” TypeError“,” stackTrace“:[[”“ /var/task/lambda_function.py”,38,“ lambda_handler”,“ print(loadFindings ['Findings'] ['Resource'] ['NetworkInterfaces'] ['PublicIp'])“]]}
My full code is below so far: 到目前为止,我的完整代码如下:
import json
import boto3
from pprint import pprint # Pretty-print for displaying the JSON nicely.
#pprint(listOfFindings)
def lambda_handler(event, context):
client = boto3.client('guardduty') # Creating the client.
Det_ID = '5ab1b6808e98faaabd947a01af9ed970' # Setting the Detect ID for GD.
response = client.list_findings(DetectorId=Det_ID) # Gathering all findings... Need to filter.
findings = json.dumps(response) # Dumping the JSON findings
listOfFindings = json.loads(findings) # Making them into a readable format for Python.
# print("Here's the IDs!",listOfFindings['FindingIds'],"\n\n\n") # Printing all Finding IDs.
idPosition=0
idList = []
for id in listOfFindings['FindingIds']: # Looping through all the Finding IDs.
#print("\n\n\nNumber", x, listOfFindings['FindingIds'][x]) # Prints all the Finding Ids separated.
idList.append(listOfFindings['FindingIds'][idPosition])
idPosition+=1
# print("TEST") - Debugging.
# print(idList) - Debugging.
findingsList = []
position = 0
for ids in idList:
# print(idList[position])
stringFindingId = str(idList[position])
#stringFindingId = idList[position]
allFindings = client.get_findings(
DetectorId=Det_ID,
FindingIds=[
stringFindingId,])
dumpFindings = json.dumps(allFindings)
loadFindings = json.loads(dumpFindings)
# findingsList.append(loadFindings)
print(loadFindings['Findings']['Resource']['NetworkInterfaces']['PublicIp']) # BROKEN HERE
position += 1
Any help is really appreciated! 任何帮助都非常感谢!
The docs show that the value for 'Findings'
is a list of dictionaries. 文档显示, 'Findings'
的值是词典列表。 So either just use allFindings['Findings'][0]
(if there's only one item in the list) or loop over allFindings['Findings']
. 因此,要么使用allFindings['Findings'][0]
(如果列表中只有一项),要么遍历allFindings['Findings']
。
By the way this code is pointless: 顺便说一句,这段代码毫无意义:
dumpFindings = json.dumps(allFindings)
loadFindings = json.loads(dumpFindings)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.