英文:
jq - add element from outside array
问题
[
"i-0af68a7cf857bd010",
"admin",
"accountsservice",
"0.6.45-1ubuntu1.3",
"accountsservice_0.6.45-1ubuntu1.3_amd64.deb",
"Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
"https://www.freedesktop.org/wiki/Software/AccountsService/",
"query and manipulate user account information"
]
感谢您的支持!
英文:
{
"TypeName": "AWS:Application",
"InstanceId": "i-0af68a7cf857bd010",
"SchemaVersion": "1.1",
"CaptureTime": "2023-03-01T16:28:43Z",
"Entries": [
{
"ApplicationType": "admin",
"Architecture": "x86_64",
"Name": "accountsservice",
"PackageId": "accountsservice_0.6.45-1ubuntu1.3_amd64.deb",
"Publisher": "Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
"Summary": "query and manipulate user account information",
"URL": "https://www.freedesktop.org/wiki/Software/AccountsService/",
"Version": "0.6.45-1ubuntu1.3"
}
]
}
This is a snippet of a large selection I'm trying to format to eventually export as csv.
.Entries[] | [.ApplicationType, .Name, .Version, .PackageId, .Publisher, .URL, .Summary]
[
"admin",
"accountsservice",
"0.6.45-1ubuntu1.3",
"accountsservice_0.6.45-1ubuntu1.3_amd64.deb",
"Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
"https://www.freedesktop.org/wiki/Software/AccountsService/",
"query and manipulate user account information"
]
Is there a way that I can insert the .InstanceId from outside the array into the array, leaving me with:
[
"i-0af68a7cf857bd010",
"admin",
"accountsservice",
"0.6.45-1ubuntu1.3",
"accountsservice_0.6.45-1ubuntu1.3_amd64.deb",
"Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
"https://www.freedesktop.org/wiki/Software/AccountsService/",
"query and manipulate user account information"
]
Thank you so much for the assistance!
</details>
# 答案1
**得分**: 1
```json
["i-0af68a7cf857bd010", "admin", "accountsservice", "0.6.45-1ubuntu1.3", "accountsservice_0.6.45-1ubuntu1.3_amd64.deb", "Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>", "https://www.freedesktop.org/wiki/Software/AccountsService/", "query and manipulate user account information"]
英文:
You could concatenate the arrays of two streams (one stream contains a single array, the other stream contains multiple arrays). This builds the cartesian product of both streams.
[.InstanceId] + (.Entries[] | [.ApplicationType, .Name, .Version, .PackageId, .Publisher, .URL, .Summary])
Output:
[
"i-0af68a7cf857bd010",
"admin",
"accountsservice",
"0.6.45-1ubuntu1.3",
"accountsservice_0.6.45-1ubuntu1.3_amd64.deb",
"Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
"https://www.freedesktop.org/wiki/Software/AccountsService/",
"query and manipulate user account information"
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论