在 sed 中使用 JQ 就地编辑 json 文件-i
我发现了很多解决方案,例如
jq ... input.json > tmp.json && mv tmp.json input.json
这可行,但我需要过滤我的文件,添加一些数据,然后将其放回原始文件中。(即更新文件中的特定对象)
我的文件包含 1000 多个具有不同Element
. 我将始终使用过滤器Element
,我缩短了问题。我有一个示例文件original.json
...
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "optouts",
"Path": "/optouts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "subscriptionTypes",
"Path": "/subscription-types",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
我想过滤以objectName
将contacts
一些数据添加到空 IBM 字段并保存到文件中
应用相同的逻辑将 IBM 字段就地编辑到"Y"
包含的对象上"objectName": "contacts"
jq 'select(.objectName == "contacts") | .IBM = "Y"' original.json > tmpjson.json && mv tmpjson.json original.json
现在我的文件original.json
显示
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "Y",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
预期结果
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "Y",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "optouts",
"Path": "/optouts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "subscriptionTypes",
"Path": "/subscription-types",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
似乎在使用 select 后我不能再使用提供的解决方案https://github.com/stedolan/jq/wiki/FAQ#general-questions