I'm working with the zendesk API and the attachments are an array under comments -- effectively:
[
"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"
]
I'm trying to select comments where the attachments are not empty.
I can select jq '.comments[].attachments[]'
, which effectively returns the correct number of attachments, but since it's possible to upload with the same name, I need the parent created_at for disambiguation.
If I use :
jq '.comments[] | select(.attachments[] > 0) | {id: .attachments[].id, date: .created_at, name: .attachments[].file_name, url: .attachments[].content_url, size: .attachments[].size}'
This appears to append attachments to every comment.
How do you effectively select the parent only when the child array is empty?
The way in which you've asked the question is very confusing to most of us, but if I understand things correctly, this will help:
.comments[]
| .created_at as $date
| .attachments[]
| {id, $date, name: .file_name, url: .content_url, size}
Notice the use of some small jq clevernesses, e.g. {id}
for {"id": .id}
and {$date}
for {"date": $date}
.