jq : parents 和 non-empty child 数组

jq : parents with non-empty child arrays

我正在使用 zendesk API,附件是注释下的一个数组——有效:

[
  "comments[].attachments[].content_type",
  "comments[].attachments[].content_url",
  "comments[].attachments[].file_name",
  "comments[].attachments[].height",
  "comments[].attachments[].id",
  "comments[].attachments[].mapped_content_url",
  "comments[].attachments[].size",
  "comments[].attachments[].thumbnails[].content_type",
  "comments[].attachments[].thumbnails[].content_url",
  "comments[].attachments[].thumbnails[].file_name",
  "comments[].attachments[].thumbnails[].height",
  "comments[].attachments[].thumbnails[].id",
  "comments[].attachments[].thumbnails[].mapped_content_url",
  "comments[].attachments[].thumbnails[].size",
  "comments[].attachments[].thumbnails[].url",
  "comments[].attachments[].thumbnails[].width",
  "comments[].attachments[].url",
  "comments[].attachments[].width",
  "comments[].audit_id",
  "comments[].author_id",
  "comments[].body",
  "comments[].created_at",
  "comments[].html_body",
  "comments[].id",
  "comments[].metadata.system.client",
  "comments[].metadata.system.ip_address",
  "comments[].metadata.system.latitude",
  "comments[].metadata.system.location",
  "comments[].metadata.system.longitude",
  "comments[].plain_body",
  "comments[].public",
  "comments[].type",
  "comments[].via.channel",
  "comments[].via.source.to.address",
  "comments[].via.source.to.name",
  "count"
]

我正在尝试 select 在附件不为空的地方发表评论。

我可以 select jq '.comments[].attachments[]',这实际上 returns 正确的附件数量,但由于可以使用相同的名称上传,我需要 parent created_at 用于消歧。

如果我使用:

jq '.comments[] | select(.attachments[] > 0) | {id: .attachments[].id, date: .created_at, name: .attachments[].file_name, url: .attachments[].content_url, size: .attachments[].size}'

这似乎是将附件附加到每个评论。

只有当child数组为空时,你如何有效地selectparent?

你问问题的方式让我们大多数人感到困惑,但如果我理解正确,这将有所帮助:

.comments[]
| .created_at as $date
| .attachments[]
| {id, $date, name: .file_name, url: .content_url, size}

注意使用一些小的 jq 技巧,例如{id} 对于 {"id": .id} {$date} {"date": $date}.