[英]In Python How do I get specific Fields from Json response?
Hello everyone here is my Json response:大家好,这里是我的 Json 回复:
{
"assets": [
{
"id": 518447,
"created_at": "2019-09-10T10:13:38Z",
"priority": 10,
"operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1",
"notes": null,
"last_booted_at": null,
"primary_locator": "external_id",
"locator": "1112359",
"vulnerabilities_count": 22,
"status": "active",
"last_seen_time": "2019-09-08T16:00:17Z",
"network_ports": [
{
"id": 33550493,
"port_number": 180,
"extra_info": "",
"hostname": null,
"name": "HTTP",
"ostype": "",
"product": "JBoss EAP",
"protocol": "tcp",
"state": "open",
"version": "4.2.3.GA"
},
{
"id": 33550494,
"port_number": 100,
"extra_info": "",
"hostname": null,
"name": "SNMP",
"ostype": "",
"product": null,
"protocol": "udp",
"state": "open",
"version": null
},
],
"tags": [
"Windows Server",
"DO - DO SPG BOM"
],
"owner": null,
"urls": {
"vulnerabilities": ""
},
"ip_address": "10.10.10.1",
"database": null,
"hostname": null,
"fqdn": null,
"netbios": null,
"application": null,
"file": null,
"mac_address": null,
"ec2": null,
"url": null,
"external_id": "1112359",
"ipv6": null,
"asset_groups": [
{
"id": 4,
"name": "0 Global - All"
},
{
"id": 204,
"name": "DO - All"
},
{
"id": 417,
"name": "Do - All"
}
]
},
What I want to do is to get specifc fields such as the tags, operating system, etc. but I do not know what logic I am supposed to implement in this case to cherry pick which piece of information I want gather.我想要做的是获取特定字段,例如标签、操作系统等,但我不知道在这种情况下我应该实施什么逻辑来挑选我想要收集的信息。
here is what I done so far这是我到目前为止所做的
import requests
import gzip
import json
url = 'https://api.thisismyurl.com/assets/'
token = 'Blahblahblah'
# 'Content-Type': 'application/json'
headers = {'X-Risk-Token': token, 'Accept': 'application/json'}
response = requests.get(url,headers=headers)
print(response.status_code)
json_format = json.loads(response.text)
here is where I dont know how to necessarily move forward in this case.这是我不知道在这种情况下必须如何前进的地方。
Any ideas?有任何想法吗?
First of all, your json is not valid is was missing closing brackets.首先,您的 json 无效是缺少右括号。 this one works:
这个有效:
{
"assets": [{
"id": 518447,
"created_at": "2019-09-10T10:13:38Z",
"priority": 10,
"operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1",
"notes": null,
"last_booted_at": null,
"primary_locator": "external_id",
"locator": "1112359",
"vulnerabilities_count": 22,
"status": "active",
"last_seen_time": "2019-09-08T16:00:17Z",
"network_ports": [{
"id": 33550493,
"port_number": 180,
"extra_info": "",
"hostname": null,
"name": "HTTP",
"ostype": "",
"product": "JBoss EAP",
"protocol": "tcp",
"state": "open",
"version": "4.2.3.GA"
},
{
"id": 33550494,
"port_number": 100,
"extra_info": "",
"hostname": null,
"name": "SNMP",
"ostype": "",
"product": null,
"protocol": "udp",
"state": "open",
"version": null
}
],
"tags": [
"Windows Server",
"DO - DO SPG BOM"
],
"owner": null,
"urls": {
"vulnerabilities": ""
},
"ip_address": "10.10.10.1",
"database": null,
"hostname": null,
"fqdn": null,
"netbios": null,
"application": null,
"file": null,
"mac_address": null,
"ec2": null,
"url": null,
"external_id": "1112359",
"ipv6": null,
"asset_groups": [{
"id": 4,
"name": "0 Global - All"
},
{
"id": 204,
"name": "DO - All"
},
{
"id": 417,
"name": "Do - All"
}
]
}]
}
once you created the variable with the json you can access the data this way:使用 json 创建变量后,您可以通过以下方式访问数据:
print(json_format['assets'])
This will return all objects in ['assets']:这将返回 ['assets'] 中的所有对象:
[{'id': 518447, 'created_at': '2019-09-10T10:13:38Z', 'priority': 10, 'operating_system': 'Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1', 'notes': None, 'last_booted_at': None, 'primary_locator': 'external_id', 'locator': '1112359', 'vulnerabilities_count': 22, 'status': 'active', 'last_seen_time': '2019-09-08T16:00:17Z', 'network_ports': [{'id': 33550493, 'port_number': 180, 'extra_info': '', 'hostname': None, 'name': 'HTTP', 'ostype': '', 'product': 'JBoss EAP', 'protocol': 'tcp', 'state': 'open', 'version': '4.2.3.GA'}, {'id': 33550494, 'port_number': 100, 'extra_info': '', 'hostname': None, 'name': 'SNMP', 'ostype': '', 'product': None, 'protocol': 'udp', 'state': 'open', 'version': None}], 'tags': ['Windows Server', 'DO - DO SPG BOM'], 'owner': None, 'urls': {'vulnerabilities': ''}, 'ip_address': '10.10.10.1', 'database': None, 'hostname': None, 'fqdn': None, 'netbios': None, 'application': None, 'file': None, 'mac_address': None, 'ec2': None, 'url': None, 'external_id': '1112359', 'ipv6': None, 'asset_groups': [{'id': 4, 'name': '0 Global - All'}, {'id': 204, 'name': 'DO - All'}, {'id': 417, 'name': 'Do - All'}]}]
You can select the next object by adding it directly after the ['assets'] object:您可以在 ['assets'] object 之后直接添加 select 下一个 object:
print(json_format['assets'][0]['ip_address'])
returns:返回:
10.10.10.1
And this goes on and on:这一直在继续:
print(json_format['assets'][0]['network_ports'][0]['version'])
returns返回
4.2.3.GA
The [0] is used as we want the data from the first occurance of the ['assets'] object and first occurrence of the network ports. [0] 用于我们想要来自第一次出现的 ['assets'] object 和第一次出现的网络端口的数据。 [1] would be the second object with this title.
[1] 将是具有此标题的第二个 object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.