【问题标题】:jq - output array as csv followed by other fields in the objectjq - 输出数组为 csv,后跟对象中的其他字段
【发布时间】:2020-07-02 21:54:02
【问题描述】:

我有以下 json 布局:

test.json

{
  "end": 9,
  "previous_page_uri": null,
  "messages": [
    {
      "error_message": null,
      "num_media": "1",
      "status": "received"
    },
    {
      "error_message": null,
      "num_media": "2",
      "status": "received"
    }
],
        "end1": "end page 1",
        "end2": "end page 2"
}

我想将 .messages 对象输出为 csv,后跟“end1”值。

有没有办法在 jq 中做到这一点?

生成 csv:

 jq '.messages[] | [.error_message, .num_media, .status]|@csv' test.json

产生这个:

",\"1\",\"received\""
",\"2\",\"received\""

如何添加 .end1?

【问题讨论】:

  • 请明确要求(您的意思是:在每行输出的末尾将“添加.end1”作为CSV值吗?),并显示预期的输出。如需进一步指导,请参阅 minimal reproducible example

标签: json jq


【解决方案1】:

您可以使用,Comma 连接两个过滤器的输出,并使用( ) Parenthesis 分别指定它们。例如,对于您提供过滤器的示例输入

( .messages[] | [.error_message, .num_media, .status] | @csv ), .end1

生成

",\"1\",\"received\""
",\"2\",\"received\""
"end page 1"

Try it online!

【讨论】:

  • 非常感谢!我知道必须有办法。我曾尝试使用逗号,但不知道 ()。
猜你喜欢
  • 2020-06-25
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多