简体   繁体   English

如何使用JQ连接json中的数组

[英]How to concatenate an array in json using JQ

trying to combine the rows.试图合并行。 Here is the source这是来源

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

Here is my command line这是我的命令行

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

Im getting the following result我得到以下结果

96
28,35,80

how do I make it 96,28,35,80 ?我如何使它成为96,28,35,80 By the way im doing this on windows command line顺便说一下,我在 Windows 命令行上执行此操作

A small tweak of your jq filter would do the trick:对 jq 过滤器稍作调整即可解决问题:

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

(The above line does not take into account your shell's rules for escaping special characters. Have you considered circumventing such issues by using the -f command-line option?) (以上行没有考虑到您的 shell 转义特殊字符的规则。您是否考虑过使用 -f 命令行选项来规避此类问题?)

An alternative替代

Here's an alternative that avoids using double quotes ( " ) in the filter:这是避免在过滤器中使用双引号 ( " ) 的替代方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM