Json 使用 Jq MAP 更新
Json update using Jq MAP
我是 JQ 的新手,我认为这应该能满足我的需求,但它抛出了无法解决的错误。
cat aws_test_query_history_pipeline.json | jq '.parameters[] | map(if
.id == "clusterName"
then . + {"id"="changed"}
else .
end
)' >> test_krish.json
jq: error: syntax error, unexpected '=', expecting '}' (Unix shell
quoting issues?) at <top-level>, line 2:
then . + {"id"="changed"}
jq: 1 compile error
我知道我遗漏了一些愚蠢的东西请让我知道我遗漏了什么。
更新这个问题,因为我遗漏了一些关于它的信息。
很抱歉。这是我的样本
{
"objects": [
{
"connectionString": "jdbc:mysql:
1.rds.amazonaws.com",
"*password": "",
"name": "Insights",
"id": "DataNodeId_XAL9t",
"type": "MySqlDataNode",
"table": "stas",
"username": "porta"
},
{
"dependsOn": {
.......
]
"parameters": [
{
"id": "clusterName",
"description": "Which region Cluster name to use",
"type": "String",
"default": ""
},
{
"id": "password",
"description": "Password to use",
"type": "String",
"default": ""
}
]
}
上面是在 file.json 上,当我 运行 这个命令时,它只是让我的参数数组按照命令中提到的进行了更改,但我还需要保持剩余的 json 文件不变
cat test.json | jq '.parameters | if .id == "clusterName" then .id =
"changed" elif .id == "password" then .id = "passchange" else . end' >
test_krish.json
上面的命令只是给我
{
"id": "changed",
"description": "Which region Cluster name to use",
"type": "String",
"default": ""
}
{
"id": "passchange",
"description": "Password to use",
"type": "String",
"default": ""
}
如错误消息所示,{"id"="changed"}
是错误的。也许你的意思是:
{"id":"changed"}
如果你确实只是想改变.id
,你可以这样写:
if .id == "clusterName" then .id = "changed" else . end
顺便说一句,如果您以后遵循 http://whosebug.com/help/mcve
中的指南就好了
修改问题的答案
(.parameters[].id |= if . == "clusterName" then "changed" else . end)
我是 JQ 的新手,我认为这应该能满足我的需求,但它抛出了无法解决的错误。
cat aws_test_query_history_pipeline.json | jq '.parameters[] | map(if
.id == "clusterName"
then . + {"id"="changed"}
else .
end
)' >> test_krish.json
jq: error: syntax error, unexpected '=', expecting '}' (Unix shell
quoting issues?) at <top-level>, line 2:
then . + {"id"="changed"}
jq: 1 compile error
我知道我遗漏了一些愚蠢的东西请让我知道我遗漏了什么。
更新这个问题,因为我遗漏了一些关于它的信息。
很抱歉。这是我的样本
{
"objects": [
{
"connectionString": "jdbc:mysql:
1.rds.amazonaws.com",
"*password": "",
"name": "Insights",
"id": "DataNodeId_XAL9t",
"type": "MySqlDataNode",
"table": "stas",
"username": "porta"
},
{
"dependsOn": {
.......
]
"parameters": [
{
"id": "clusterName",
"description": "Which region Cluster name to use",
"type": "String",
"default": ""
},
{
"id": "password",
"description": "Password to use",
"type": "String",
"default": ""
}
]
}
上面是在 file.json 上,当我 运行 这个命令时,它只是让我的参数数组按照命令中提到的进行了更改,但我还需要保持剩余的 json 文件不变
cat test.json | jq '.parameters | if .id == "clusterName" then .id =
"changed" elif .id == "password" then .id = "passchange" else . end' >
test_krish.json
上面的命令只是给我
{
"id": "changed",
"description": "Which region Cluster name to use",
"type": "String",
"default": ""
}
{
"id": "passchange",
"description": "Password to use",
"type": "String",
"default": ""
}
如错误消息所示,{"id"="changed"}
是错误的。也许你的意思是:
{"id":"changed"}
如果你确实只是想改变.id
,你可以这样写:
if .id == "clusterName" then .id = "changed" else . end
顺便说一句,如果您以后遵循 http://whosebug.com/help/mcve
中的指南就好了修改问题的答案
(.parameters[].id |= if . == "clusterName" then "changed" else . end)