【问题标题】:How to concatenate an array in json using JQ如何使用 JQ 连接 json 中的数组
【发布时间】:2020-02-24 02:03:43
【问题描述】:

尝试合并行。这是来源

{
  "movie_results": [
    {
      "genre_ids": [
        28,
        35,
        80
      ],
      "id": 96,
    }
  ]

这是我的命令行

C:\WINDOWS\system32>curl "https://api.themoviedb.org/3/find/tt0092644?&external_source=imdb_id" | jq -r ".movie_results[] | .id, (.genre_ids | join(\",\"))

我得到以下结果

96
28,35,80

我如何做到96,28,35,80?顺便说一句,我在 Windows 命令行上这样做

【问题讨论】:

    标签: json windows batch-file export-to-csv jq


    【解决方案1】:

    你的 jq 过滤器的一个小调整就可以了:

    .movie_results[] | [.id, .genre_ids[]] | join(",")
    

    (上面的行没有考虑你的shell对特殊字符转义的规则。你有没有考虑通过使用-f命令行选项来规避这些问题?)

    另一种选择

    这是避免在过滤器中使用双引号 (") 的替代方法:

    .movie_results[] | [.id] + .genre_ids | @csv
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2021-06-29
      相关资源
      最近更新 更多