【问题标题】:Python Getting Specific JSON values from AWS GuardDutyPython 从 AWS GuardDuty 获取特定的 JSON 值
【发布时间】:2018-11-04 01:51:54
【问题描述】:

我在 AWS Lambda 上编写了一些代码,并试图从 GuardDuty 的调查结果中提取 IP。我的 FindingId 没问题,但是当我尝试提取 IP 地址时,我收到以下错误:

{ "errorMessage": "列表索引必须是整数或切片,不能 str”,“errorType”:“TypeError”,“stackTrace”:[ [ "/var/task/lambda_function.py", 38, "lambda_handler", “打印(loadFindings['Findings']['Resource']['NetworkInterfaces']['PublicIp'])” ] ] }

到目前为止,我的完整代码如下:

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

非常感谢任何帮助!

【问题讨论】:

  • 你能发一个loadFindings 的样本吗?

标签: python json amazon-web-services aws-lambda


【解决方案1】:

docs 表明'Findings' 的值是一个字典列表。所以要么只使用allFindings['Findings'][0](如果列表中只有一项),要么循环使用allFindings['Findings']

顺便说一句,这段代码毫无意义:

    dumpFindings = json.dumps(allFindings)
    loadFindings = json.loads(dumpFindings)

【讨论】:

  • 非常感谢!
  • 代码怎么没有意义?它返回一个 JSON 字典,需要将其转换为 Python 可处理的。仍然得到同样的错误。
  • @SamPitman 它只是返回一个字典,我不知道你从哪里得到 JSON 的想法。更一般地说,json.loads(json.dumps(x)) 总是等于 x
  • 我的错,看到其他人这样做,但似乎他们不知道他们在做什么!两个快速的问题,在 '['Findings']' 之后需要什么 '[0]' 并且这行代码不起作用: pprint(allFindings['Findings'][0]['Resource']['InstanceDetails' ]['NetworkInterfaces']['PublicIp']) - 在没有 ['PublicIp'] 的情况下工作,但不能使用它。
  • allFindings['Findings'] 是一个列表,allFindings['Findings'][0] 是该列表的第一个元素。 allFindings[...]['NetworkInterfaces'] 又是一个列表(请注意文档中响应语法中 { 之前的 [,或者只是打印内容以查看它们是什么),您应该循环查找正确的接口。
猜你喜欢
  • 2022-10-20
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2021-04-21
相关资源
最近更新 更多