Python 从json对象中排除或过滤json键并写入文件
我目前正在使用一个python模块,该模块使用tenable API从tenable导出资产数据。export函数返回一个“ExportIterator”类型的对象以遍历导出结果 从本质上讲,每个资产返回的数据太多,我很难弄清楚如何过滤出返回的数据以便使用它 该模块以提取资产信息为例,展示了以下功能:Python 从json对象中排除或过滤json键并写入文件,python,json,Python,Json,我目前正在使用一个python模块,该模块使用tenable API从tenable导出资产数据。export函数返回一个“ExportIterator”类型的对象以遍历导出结果 从本质上讲,每个资产返回的数据太多,我很难弄清楚如何过滤出返回的数据以便使用它 该模块以提取资产信息为例,展示了以下功能: assets = tio.exports.assets() for asset in assets: print(asset) 这将返回数千个json对象,其中包含数百个键(我已经删除并混淆
assets = tio.exports.assets()
for asset in assets:
print(asset)
这将返回数千个json对象,其中包含数百个键(我已经删除并混淆了几个键),如下所示:
{
"id": "1a2b3c",
"has_plugin_results": true,
"created_at": "xxx",
"terminated_at": null,
"terminated_by": null,
"updated_at": "xxx",
"deleted_at": null,
"deleted_by": null,
"first_seen": "",
"last_seen": "",
"first_scan_time": "xxx",
"last_scan_time": "xxx",
"last_authenticated_scan_date": "xxx",
"last_licensed_scan_date": "xxx,
"last_scan_id": "xxx,
"last_schedule_id": "xxx",
"azure_vm_id": null,
"azure_resource_id": null,
"gcp_project_id": null,
"gcp_zone": null,
"gcp_instance_id": null,
"aws_ec2_instance_ami_id": null,
"aws_ec2_instance_id": null,
"agent_uuid": "xxx",
"bios_uuid": "xxx",
"network_id": "xxx",
"network_name": "Default",
"aws_owner_id": null,
"aws_availability_zone": null,
"aws_region": null,
"aws_vpc_id": null,
"aws_ec2_instance_group_name": null,
"aws_ec2_instance_state_name": null,
"aws_ec2_instance_type": null,
"aws_subnet_id": null,
"aws_ec2_product_code": null,
"aws_ec2_name": null,
"mcafee_epo_guid": "{xxx}",
"mcafee_epo_agent_guid": "{xxx}",
"servicenow_sysid": null,
"agent_names": [
"aaabbbccc123"
],
"installed_software": [],
"ipv4s": [
"1.1.1.1",
"2.2.2.2"
],
"ipv6s": [],
"fqdns": [
"aaabbbbccc"
],
"mac_addresses": [
"aa:bb:cc"
],
"netbios_names": [
"aaabbbccc123"
],
"operating_systems": [
"foobar 10"
],
"system_types": [
"general-purpose"
],
"hostnames": [
"aaabbbccc123"
],
"sources": [
{
"name": "AGENT",
"first_seen": "xxx",
"last_seen": "xxx"
}
],
}
此导出模块函数不支持任何过滤json对象本身的参数。我只需要一个json文件,其中列出了带有“主机名”和“ipv4s”的这些对象。以下是我当前的代码,用于尝试将此资产数据过滤到一个json文件,其中只包含我想要的key:value元素:
from tenable.io import TenableIO
import json
import os
# api-key as an environment variable
tio = TenableIO()
# doc for pytenable exports function https://pytenable.readthedocs.io/en/stable/io.html#exports
wr = open('assets.json','w')
#create variable to iterate through the export function results
for asset in tio.exports.assets:
new_data = [{ "hostnames" : x["hostnames"], "ipv4s" : x["ipv4s"]} for x in asset[{}]]
wr.write(json.dumps((new_data), indent = 4))
wr.close()
目前收到:
new_data = [{ "hostnames" : x["hostnames"], "ipv4s" : x["ipv4s"]} for x in asset[{}]]
TypeError: unhashable type: 'dict'
期望输出:
{
"hostnames": [
"aaabbbccc123"
],
"ipv4s": [
"1.1.1.1",
"2.2.2.2"
],
}
{
"hostnames": [
"dddeeefff456"
],
"ipv4s": [
"1.1.1.1",
"2.2.2.2"
],
}
有关于如何继续的想法吗?资产应该已经是一个json对象了。在大多数Python Json解析库中,Json对象被转换为字典,因此您可以执行以下操作:
for asset in tio.exports.assets:
new_data = [{ "hostnames" : asset["hostnames"], "ipv4s" : asset["ipv4s"]}]
wr.write(json.dumps((new_data), indent = 4))
wr.close()
如果不是您收到的每个json blob都有“hostnames”和“ipv4s”键,那么我建议在尝试访问它之前使用以下命令检查该键是否存在:
if "hostnames" in asset:
# add hostnames to the new dictionary here
这立刻奏效了。非常感谢。