如何将以下字典转换为特定的 JSON 结构?

How would you convert the following dictionary to a specific JSON structure?

到目前为止,我还不是 Python3 英雄,但专注于使用它学习一些新技能,因此,我们将不胜感激。在我想稍后投入 GitHub 的个人项目中,我 运行 有一个命令输出以下 Python 字典:

{'masscan': {'command_line': 'masscan -oX - 192.168.0.131/24 -p 22,80 --max-rate=1000', 'scanstats': {'timestr': '2022-03-26 10:00:07', 'elapsed': '12', 'uphosts': '2', 'downhosts': '0', 'totalhosts': '2'}}, 'scan': {'192.168.0.254': {'tcp': {80: {'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '64', 'endtime': '1648285195', 'services': []}, 22: {'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '64', 'endtime': '1648285195', 'services': []}}}}}

然后我想将其解析为以下 JSON 格式:

{
"data": [
    {
        "{#PORT}": 80,
        "{#STATE}": "OPEN",
        "{#ENDTIME}": "1648285195"
    },
    {
        "{#PORT}": 22,
        "{#STATE}": "OPEN",
        "{#ENDTIME}": "1648285195"
    }
]
}  

解析它的最有效方法是什么?我不希望它最终出现在文件中,但最好将其保存在我的代码中。请记住,可能有更多的端口,而不仅仅是端口 22 和 80。字典可能更长,但遵循相同的格式。

谢谢!

这个函数 return 正是你想要的(我想):

def parse_data(input):
    data = []
    for ip in input['scan'].keys():
        for protocol in input['scan'][ip].keys():
            for port in input['scan'][ip][protocol].keys():
                port_data = {"{#PORT}": port, "{#STATE}": input['scan'][ip][protocol][port]['state'].upper(), "{#ENDTIME}": input['scan'][ip][protocol][port]['endtime']}
                data.append(port_data)
    return {'data': data} 

函数returns(输出):

    {
   "data":[
      {
         "{#PORT}":80,
         "{#STATE}":"OPEN",
         "{#ENDTIME}":"1648285195"
      },
      {
         "{#PORT}":22,
         "{#STATE}":"OPEN",
         "{#ENDTIME}":"1648285195"
      }
   ]
}

不知道端口“22”'state' 中的 'Interface #2' 来自哪里(在您想要的结果中)。

可能的解决方案如下:

log_data = {'masscan': {'command_line': 'masscan -oX - 192.168.0.131/24 -p 22,80 --max-rate=1000', 'scanstats': {'timestr': '2022-03-26 10:00:07', 'elapsed': '12', 'uphosts': '2', 'downhosts': '0', 'totalhosts': '2'}}, 'scan': {'192.168.0.254': {'tcp': {80: {'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '64', 'endtime': '1648285195', 'services': []}, 22: {'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '64', 'endtime': '1648285195', 'services': []}}}}}

result = {"data": []}

for k, v in dct['scan'].items():
    for tcp, tcp_data in v.items():
        for port, port_data in tcp_data.items():
            data = {"{#PORT}": port, "{#STATE}": port_data['state'], "{#ENDTIME}": port_data['endtime']}
            result["data"].append(data)
            
print(result)

版画

{'data': [
    {'{#PORT}': 80, '{#STATE}': 'open', '{#ENDTIME}': '1648285195'},
    {'{#PORT}': 22, '{#STATE}': 'open', '{#ENDTIME}': '1648285195'}]}

您可以对 'tcp' 键进行递归搜索,然后从那里开始。像这样:

mydict = {'masscan': {'command_line': 'masscan -oX - 192.168.0.131/24 -p 22,80 --max-rate=1000', 'scanstats': {'timestr': '2022-03-26 10:00:07', 'elapsed': '12', 'uphosts': '2', 'downhosts': '0', 'totalhosts': '2'}},
          'scan': {'192.168.0.254': {'tcp': {80: {'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '64', 'endtime': '1648285195', 'services': []}, 22: {'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '64', 'endtime': '1648285195', 'services': []}}}}}


def findkey(d, k):
    if k in d:
        return d[k]
    for v in d.values():
        if isinstance(v, dict):
            if r := findkey(v, k):
                return r


rdict = {'data': []}
for k, v in findkey(mydict, 'tcp').items():
    rdict['data'].append(
        {'{#PORT}': k, '{#STATE}': v['state'].upper(), '{#ENDTIME}': v['endtime']})


print(rdict)

输出:

{'data': [{'{#PORT}': 80, '{#STATE}': 'OPEN', '{#ENDTIME}': '1648285195'}, {'{#PORT}': 22, '{#STATE}': 'OPEN', '{#ENDTIME}': '1648285195'}]}