gpt4 book ai didi

bash - 用jq添加json数组元素(命令行)

转载 作者:行者123 更新时间:2023-11-29 08:51:28 26 4
gpt4 key购买 nike

<分区>

我正在尝试在 bash 中生成一个 json 文件。我安装了 jq,希望它能帮助我生成和附加 json。

比如我要生成这样格式的json:

{
"Project": [
{
"projectName": {
"branch": [
{
"branchName": [
"path"
]
}
],
"tag": [
{
"tagName": [
"path"
]
}
]
}
}
]
}

虽然可以用下面的过滤器做类似的事情

 .Project=.Project+.Project+
[{"projectName" : {"branch" : (.branch+[{"branchName":(.tagName+["path"])}]),
"tag": (.tag+[{"tagName":(.tagName+["path"])}]) }}]

当我想在同一个项目和名称中创建另一个条目时,它会创建一个全新的条目,如果它是一个新项目,结果是:

    {
"Project": [
{
"projectName": {
"branch": [
{
"branchName": [
"path"
]
}
],
"tag": [
{
"tagName": [
"path"
]
}
]
}
},
{
"projectName": {
"branch": [
{
"branchName": [
"path"
]
}
],
"tag": [
{
"tagName": [
"path"
]
}
]
}
},
{
"projectName": {
"branch": [
{
"branchName": [
"path2"
]
}
],
"tag": [
{
"tagName": [
"path2"
]
}
]
}
}
]
}

但我想拥有

{
"Project": [
{
"projectName": {
"branch": [
{
"branchName": [
"path",
"path2"
]
}
],
"tag": [
{
"tagName": [
"path",
"path2"
]
}
]
}
}
]
}

jq/bash 有办法吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com