JSON 使用 jq 将文件转换为 CSV 文件

JSON file to CSV file conversion using jq

我正在尝试使用 jq 将我的 json 文件转换为 csv 文件。下面是示例输入 events.json 文件。

{
  "took" : 111,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "alerts",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "alertID" : "639387c3-0fbe-4c2b-9387-c30fbe7c2bc6",
          "alertCategory" : "Server Alert",
          "description" : "Successfully started.",
          "logId" : null
          }
       },
       {
        "_index" : "alerts",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "alertID" : "2",
          "alertCategory" : "Server Alert",
          "description" : "Successfully stoped.",
          "logId" : null
          }
       }
   ]
  }
}

我在 csv 中的行应该在每个 _source 标签中包含数据。所以我的专栏将是 alertIdalertCategorydescriptionlogId 及其各自的数据。

我尝试了以下命令: jq --raw-output '.hits[] | [."alertId",."alertCategory",."description",."logId"] | @csv' < /root/events.json 而且它不起作用。

谁能帮我解决这个问题?

您的 path-expression 不正确,您在名为 hits 的 object 中有一个 hits 数组,并且您尝试放入 CSV 中的字段存在于 __source object.

所以你的表情应该在下面。将它与 -r 标志一起使用以将输出置于原始输出格式

.hits.hits[]._source | [ .alertID, .alertCategory, .description, .logId ] | @csv

如果您的字段是 null,则您的空字段值的字符串表示结果仅为 ""。如果您想要显式 "null" 字符串表示,请使用替代运算符以及您希望为空的字段,例如你可以 (.logId // "null")

而不是 .logId

要在输出 CSV 格式中添加列名作为 header,您可以在原始输出格式 -r[中使用 @csvjoin(",") 函数

[ "alertId" , "alertCategory" , "description", "logId" ], 
( .hits.hits[]._source |  [ .alertID, .alertCategory, .description, .logId // "null" ]) | @csv

[ "alertId" , "alertCategory" , "description", "logId" ], 
( .hits.hits[]._source |  [ .alertID, .alertCategory, .description, .logId // "null" ]) | join(",")