【问题标题】:Json update using Jq MAP使用 Jq MAP 更新 Json
【发布时间】:2017-06-29 17:41:59
【问题描述】:

我是 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": ""
}

【问题讨论】:

    标签: json edit jq


    【解决方案1】:

    如错误消息所示,{"id"="changed"} 是错误的。也许你的意思是:

    {"id":"changed"}
    

    如果你确实只是想改变.id,你可以写:

    if .id == "clusterName" then .id = "changed" else . end
    

    顺便说一句,如果你在未来遵循http://stackoverflow.com/help/mcve 的指导方针会很好

    修改问题的答案

    (.parameters[].id |= if . == "clusterName" then "changed" else . end)
    

    【讨论】:

    • 感谢这有帮助,但在我的 JSON 中,我有两个数组,当我在 aws_test_query_history_pipeline.json 运行查询时 | jq .参数[] | if .id == "clusterName" then .id = "changed" else 。 end' > test_krish.json 它在输出中包含 objects[] 数组我怎么能把它包含在输出文件中
    • 正如我所说,您应该遵循 mcve 指南。特别是,您应该包含一个有代表性的输入样本。
    • 非常感谢您的回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2014-03-24
    相关资源
    最近更新 更多