json - 使用jq将json文件转换为csv

标签 json csv jq

我正在使用curl从他们的API中提取Alien Vault OTX脉冲,我收到的初始输出是json格式,我需要将此json转换为csv,以便它可以被其他软件读取。我的目标是使用 jq,因为许多其他人都推荐它。

{   "count": 1210,   "next": "https://otx.alienvault.com/api/v1/pulses/subscribed?page=2",   "results": [
    {
      "industries": [],
      "tlp": "white",
      "description": "Tropic Trooper (also known as KeyBoy) levels its campaigns against Taiwanese, Philippine, and Hong Kong targets, focusing on their government, healthcare, transportation, and high-tech industries. Its operators are believed to be very organized and develop their own cyberespionage tools that they fine-tuned in their recent campaigns. Many of the tools they use now feature new behaviors, including a change in the way they maintain a foothold in the targeted network.",
      "created": "2018-03-14T17:24:48.014000",
      "tags": [
        "china",
        "keyboy",
        "tropic trooper"
      ],
      "modified": "2018-03-14T17:24:48.014000",
      "author_name": "AlienVault",
      "public": 1,
      "extract_source": [],
      "references": [
        "https://blog.trendmicro.com/trendlabs-security-intelligence/tropic-trooper-new-strategy/"
      ],
      "targeted_countries": [],
      "indicators": [
        {
          "indicator": "CVE-2018-0802",
          "description": "",
          "created": "2018-03-14T17:25:03",
          "title": "",
          "content": "",
          "type": "CVE",
          "id": 406248965
        },
        {
          "indicator": "fb9c9cbf6925de8c7b6ce8e7a8d5290e628be0b82a58f3e968426c0f734f38f6",
          "description": "",
          "created": "2018-03-14T17:25:03",
          "title": "",
          "content": "",
          "type": "FileHash-SHA256",
          "id": 438581959
        }
      ],
      "more_indicators": false,
      "revision": 1,
      "adversary": "Tropic Trooper",
      "id": "5aa95ae02781860367e354e4",
      "name": "Tropic Troopers New Strategy"
    }

我希望使用 jq 提取某些字段并转换为 csv。我的预期输出如下所示:

"CVE-2018-0802","CVE"
"tibetnews.today","domain"
"02281e26e89b61d84e2df66a0eeb729c5babd94607b1422505cd388843dd5456","FileHash-SHA256"

到目前为止我已经尝试过:

<AV.json jq -r '.results.indicators[] | [.indicator, .type] | @csv' AV.csv

非常感谢任何帮助。

干杯,

乔治

最佳答案

.results 是一个数组,因此您也必须扩展它。这可以通过以下方式完成:

 .results[] | .indicators[] | [.indicator, .type] | @csv

或更紧凑:

 .results[].indicators[] | [.indicator, .type] | @csv

您还必须将输出定向到指定文件,例如:

  jq -r -f program.jq < AV.json > AV.csv

输出

"CVE-2018-0802","CVE"
"fb9c9cbf6925de8c7b6ce8e7a8d5290e628be0b82a58f3e968426c0f734f38f6","FileHash-SHA256"

关于json - 使用jq将json文件转换为csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49300056/

相关文章:

java - jackson 不会忽略未注释的字段

python - 使用 numpy 将带有标题的列表列表转换为 csv

MySQL 无法检测 jq 生成的 CSV 中的换行符

json - 我在 jq 中使用相同的语法来更改 JSON 值,但一种情况有效,而另一种情况则 bash 交互,我该如何解决这个问题?

json - jq:如何将平面 json 转换为嵌套 json

PHP从输入插入到mysql变量值

Flash 连接到 WCF 服务

sql - 使用\copy 命令将 CSV 导入 Postgres 时如何转义\n(换行符)?

ios - Objective-C中如何解析JSON响应的数据?

ruby-on-rails - 将动态生成的文件作为电子邮件发送