如何将特定的JSON文件转换为CSV文件[已关闭]

svmlkihl  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(182)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2天前关闭。
这篇文章是昨天编辑并提交审查的。
Improve this question
我正在尝试导出一个非常大的JSON文件(zip压缩:full_ip-port.json)转换为CSV。
该文件由一个具有常规结构的JSON对象组成。以下是代表性提取物(通过[to_entries[0,1]] | from_entries获得):

{
  "1122595": [
    {
      "ioc_value": "103.100.159.212:443",
      "ioc_type": "ip:port",
      "threat_type": "botnet_cc",
      "malware": "win.cobalt_strike",
      "malware_alias": "Agentemis,BEACON,CobaltStrike,cobeacon",
      "malware_printable": "Cobalt Strike",
      "first_seen_utc": "2023-05-27 02:31:39",
      "last_seen_utc": null,
      "confidence_level": 100,
      "reference": null,
      "tags": "CLOUDIE-AS-AP Cloudie Limited,CobaltStrike,cs-watermark-666666",
      "anonymous": "0",
      "reporter": "drb_ra"
    }
  ],
  "1122593": [
    {
      "ioc_value": "167.172.72.193:23",
      "ioc_type": "ip:port",
      "threat_type": "botnet_cc",
      "malware": "elf.bashlite",
      "malware_alias": "gayfgt,Gafgyt,qbot,torlus,lizkebab",
      "malware_printable": "Bashlite",
      "first_seen_utc": "2023-05-27 01:40:04",
      "last_seen_utc": null,
      "confidence_level": 75,
      "reference": "https://bazaar.abuse.ch/sample/6c901ba15327da68159712a9726807fb08868309726c55ef202818bfde22a5a7/",
      "tags": "Gafgyt",
      "anonymous": "0",
      "reporter": "abuse_ch"
    }
  ]
}

到目前为止我有:
内部部件:

jq -r '.[] | .[] | to_entries | map(.value) | @csv' full_ip-port.json

ID缺失:

jq -r '{id: (. | keys[])} | to_entries | map(.value) | @csv' full_ip-port.json

如何将其合并为一个漂亮而干净的CSV文件?

4ngedf3f

4ngedf3f1#

既然你的例子足够统一,你可以这样做:

jq -r '
  to_entries
  | (.[0].value[0]|keys_unsorted) as $keys
  | .[]
  | .key as $key
  | .value[]
  | [$key, .[$keys[]] ]
  | @csv
'

如果输入的话,第一行将是:

"1122595","103.100.159.212:443","ip:port","botnet_cc","win.cobalt_strike","Agentemis,BEACON,CobaltStrike,cobeacon","Cobalt Strike","2023-05-27 02:31:39",,100,,"CLOUDIE-AS-AP Cloudie Limited,CobaltStrike,cs-watermark-666666","0","drb_ra"

你也可以使用["key"] + $keys(例如)来生成一个CVS头:

to_entries
| (.[0].value[0]|keys_unsorted) as $keys
| ["key"] + $keys,
  (.[]
   | .key as $key
   | .value[]
  | [$key, .[$keys[]] ])
| @csv

相关问题