简体   繁体   English

如何针对特定字段的所有键值对遍历 json object?

[英]How to iterate through json object for all key value pairs of specific fields?

I have this 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"
        }
      ]
    }

I have tried it my way going through the first index[0] but I know that there are better ways to go about this我已经通过第一个索引 [0] 尝试过,但我知道 go 有更好的方法来解决这个问题

 import request
import json

url = 'https://thisismyurl.com/assets/'
token = 'blahblah'
headers = {'X-Risk-Token': token, 'Accept': 'application/json'}
response = requests.get(url,headers=headers)
print(response.status_code)
json_format = json.loads(response.text)
for a in  json_format['assets']:
     for key, value in json_format:
print('operating_system : ' + json_format['assets'][0]['operating_system'] + ' , ' + 'ip_address : ' + json_format['assets'][0]['ip_address'] + 'tags : ' + json_format['assets'][0]['tags'])

but my way has not produced the expected output I wanted.但我的方式并没有产生我想要的预期 output。

I just want go through the entire json finding each occurrence of operating system, ip address and tags我只想 go 通过整个 json 找到每个出现的操作系统,ip 地址和标签

The desired output i want is:我想要的所需 output 是:

"operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1", "tags":  "Windows Server" "DO - DO SPG BOM" , "ip_address": "10.10.10.1".

How can i do it with Python?我怎么能用 Python 做到这一点?

There are multiple parts of your code that could be responsible for the error.您的代码有多个部分可能导致错误。 I wrote some code that gives the appropriate result, the only difference is i'm reading the JSON from a file.我写了一些给出适当结果的代码,唯一的区别是我正在从文件中读取 JSON。

Code:代码:

import json
import csv

with open('../resources/web_json_test.json', 'r') as file_1:
    json_res: dict = json.loads(file_1.read())

with open('../out/json_to_csv_out.csv', 'w', newline='') as file_1:
    csv_writer = csv.DictWriter(file_1, fieldnames=['operating_system', 'tags', 'ip_address'])
    csv_writer.writeheader()
    for curr in json_res['assets']:
        csv_writer.writerow(
            {'operating_system': curr["operating_system"], 'tags': curr["tags"], 'ip_address': curr["ip_address"]})

Output: Output:

operating_system,tags,ip_address操作系统、标签、IP 地址
"Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1","['Windows Server', 'DO - DO SPG BOM']",10.10.10.1 “Microsoft - Windows - Windows Server 2008 R2,企业版 - SP1”,“['Windows Server','DO - DO SPG BOM']”,10.10.10.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM