仅打印数组属性中对象的一个​​ 属性 以及作为 jq 中数组 属性 兄弟的 属性

Print only one property of an object that is within an an array attribute as well as a property that is a sibling to the array property in jq

我有一个 json 文件,如下所示:

[
  {
    "code": "1234", 
    "files": [
        {
            "fileType": "pdf",
            "url": "http://.../a.pdf"
        },
        {
            "fileType": "video",
            "url": "http://.../b.mp4"
        }
    ]
  },
  {
    "code": "4321", 
    "files": [
        {
            "fileType": "pdf",
            "url": "http://.../c.pdf"
        },
        {
            "fileType": "video",
            "url": "http://.../d.mp4"
        }
    ]
  },
  {
    "code": "9999", 
    "files": [
        {
            "fileType": "pdf",
            "url": "http://.../e.pdf"
        }
    ]
  }
]

我只想打印 files 数组中 fileType == video 的文件,这样我最终得到的输出如下所示:

1234, "http://.../b.mp4"
4321, "http://.../d.mp4"

到目前为止,我只能输出如下所示的内容:

1234, "http://.../a.pdf", "http://.../b.mp4",
4321, "http://.../c.pdf", "http://.../d.mp4"

使用以下内容:

jq -r '.[] | select(.files[]?.fileType == "video") | [.code, .files[].url] | @csv'

我想知道如何在输出时根据 fileType 过滤 .files[]

假设理解基本语法和 -r command-line 选项,以下管道使解决方案相当 self-explanatory:

< input.json jq -r '
  .[]
  | .code as $code
  | .files[]
  | select(.fileType == "video")
  | "\($code), \"\(.url)\""
'