谷歌安全指挥中心-如何以JSON格式导出搜索结果

谷歌安全指挥中心-如何以JSON格式导出搜索结果,json,security,command,center,Json,Security,Command,Center,使用此示例: from google.cloud import securitycenter # Create a client. client = securitycenter.SecurityCenterClient() # organization_id is the numeric ID of the organization. e.g.: # organization_id = "111122222444" org_name = "organizati

使用此示例:

from google.cloud import securitycenter

# Create a client.
client = securitycenter.SecurityCenterClient()

# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources.  You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
finding_result_iterator = client.list_findings(all_sources)
for i, finding_result in enumerate(finding_result_iterator):
    print(
        "{}: name: {} resource: {}".format(
            i, finding_result.finding.name, finding_result.finding.resource_name
        )
    )
我想将所有查找导出为JSON数组,但是type(finding_result.finding)返回: 类“google.cloud.securitycenter_v1.types.Finding”

使用json.dumps(finding_result.finding)会导致一个错误,即它不可json序列化

使用gcloudsdk,通过指定“-format json”

可以实现这一点

您必须从google.cloud.securitycenter导入列表查找响应添加以下导入

然后在循环中添加以下行

ListFindingsResponse.ListFindingsResult.to_json(finding_result)

因此,解决方案应如下所示

from google.cloud import securitycenter
from google.cloud.securitycenter import ListFindingsResponse

# Create a client.
client = securitycenter.SecurityCenterClient()

# organization_id is the numeric ID of the organization. e.g.:
# organization_id = "111122222444"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources.  You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
finding_result_iterator = client.list_findings(all_sources)
for i, finding_result in enumerate(finding_result_iterator):
    print(
        ListFindingsResponse.ListFindingsResult.to_json(finding_result)
    )
    print(
        "{}: name: {} resource: {}".format(
            i, finding_result.finding.name, finding_result.finding.resource_name
        )
    )

美好的就这样。