我要删除方括号,如本例所示:
输入文件:file1.json
{ "regex_features": [
[ // This bracket needs to be removed
{
"name": "jobname",
"pattern": "[A-Z]+-[0-9]"
},
{
"name": "project",
"pattern": "[A-Z]{4}"
},
{
"name": "summary",
"pattern": "[a-z]+[a-z]+[a-z]{4}"
},
{
"name": "description",
"pattern": "[a-z]+[a-z]+[a-z]+[a-z]{4}"
}
] // this bracket needs to be removed.
]
}
请提出建议。
答案 0 :(得分:2)
假设您有这个文件。json:
{
"name": "foo",
"regex_features": [
[
{
"name": "jobname",
"pattern": "[A-Z]+-[0-9]"
},
{
"name": "project",
"pattern": "[A-Z]{4}"
}
]
]
}
您可以使用更新分配运算符将列表替换为其第一个元素(内部列表):
jq '.regex_features|=.[0]' file.json
输出:
{
"name": "foo",
"regex_features": [
{
"name": "jobname",
"pattern": "[A-Z]+-[0-9]"
},
{
"name": "project",
"pattern": "[A-Z]{4}"
}
]
}
答案 1 :(得分:0)
这是使用 jtc
Unix实用程序的替代解决方案:
bash $ <file1.json jtc -w'[0][0]' -w[0] -s
{
"regex_features": [
{
"name": "jobname",
"pattern": "[A-Z]+-[0-9]"
},
{
"name": "project",
"pattern": "[A-Z]{4}"
},
{
"name": "summary",
"pattern": "[a-z]+[a-z]+[a-z]{4}"
},
{
"name": "description",
"pattern": "[a-z]+[a-z]+[a-z]+[a-z]{4}"
}
]
}
bash $
或另一种方式:bash $ <file1.json jtc -w'[0]' -u'[0][0]'