jq – 从外部数组添加元素

huangapple go评论70阅读模式
英文:

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"
]

感谢您的支持!

英文:
{
    &quot;TypeName&quot;: &quot;AWS:Application&quot;,
    &quot;InstanceId&quot;: &quot;i-0af68a7cf857bd010&quot;,
    &quot;SchemaVersion&quot;: &quot;1.1&quot;,
    &quot;CaptureTime&quot;: &quot;2023-03-01T16:28:43Z&quot;,
    &quot;Entries&quot;: [
        {
            &quot;ApplicationType&quot;: &quot;admin&quot;,
            &quot;Architecture&quot;: &quot;x86_64&quot;,
            &quot;Name&quot;: &quot;accountsservice&quot;,
            &quot;PackageId&quot;: &quot;accountsservice_0.6.45-1ubuntu1.3_amd64.deb&quot;,
            &quot;Publisher&quot;: &quot;Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;&quot;,
            &quot;Summary&quot;: &quot;query and manipulate user account information&quot;,
            &quot;URL&quot;: &quot;https://www.freedesktop.org/wiki/Software/AccountsService/&quot;,
            &quot;Version&quot;: &quot;0.6.45-1ubuntu1.3&quot;
        }
     ]
}

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]
[
  &quot;admin&quot;,
  &quot;accountsservice&quot;,
  &quot;0.6.45-1ubuntu1.3&quot;,
  &quot;accountsservice_0.6.45-1ubuntu1.3_amd64.deb&quot;,
  &quot;Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;&quot;,
  &quot;https://www.freedesktop.org/wiki/Software/AccountsService/&quot;,
  &quot;query and manipulate user account information&quot;
]

Is there a way that I can insert the .InstanceId from outside the array into the array, leaving me with:

[
  &quot;i-0af68a7cf857bd010&quot;,
  &quot;admin&quot;,
  &quot;accountsservice&quot;,
  &quot;0.6.45-1ubuntu1.3&quot;,
  &quot;accountsservice_0.6.45-1ubuntu1.3_amd64.deb&quot;,
  &quot;Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;&quot;,
  &quot;https://www.freedesktop.org/wiki/Software/AccountsService/&quot;,
  &quot;query and manipulate user account information&quot;
]

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:

[
  &quot;i-0af68a7cf857bd010&quot;,
  &quot;admin&quot;,
  &quot;accountsservice&quot;,
  &quot;0.6.45-1ubuntu1.3&quot;,
  &quot;accountsservice_0.6.45-1ubuntu1.3_amd64.deb&quot;,
  &quot;Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;&quot;,
  &quot;https://www.freedesktop.org/wiki/Software/AccountsService/&quot;,
  &quot;query and manipulate user account information&quot;
]

huangapple
  • 本文由 发表于 2023年3月4日 01:18:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定