2

我在跑:

$ jq ".environments[] | select(.name | contains(\"docker\")) | .database.database_config.host = \"${DGRAPHIP}\"" weaviate.conf.json

在这个文件上:https ://github.com/weaviate/weaviate/blob/develop/weaviate.conf.json

这个的输出是(如果DGRAPHIP = 1.2.3.4):

{
  "name": "docker",
  "database": {
    "name": "dgraph",
    "database_config": {
      "host": "1.2.3.4", <== THIS IS GOOD!
      "port": 9080
    }
  },
  "schemas": {
    "Thing": "https://raw.githubusercontent.com/weaviate/weaviate-semantic-schemas/master/weaviate-Thing-ontology-s
chema_org.min.json",
    "Action": "https://raw.githubusercontent.com/weaviate/weaviate-semantic-schemas/master/weaviate-Action-ontology
-schema_org.min.json"
  },
  "mqttEnabled": false
}

这不是预期的结果,我希望将结果封装为:

{
    "environments": [{
        RESULTS
    }]
}
4

2 回答 2

3

简单调整:

jq --arg dgraphip "1.2.3.4" '{"environments" : .environments 
 | map(select(.name | contains("docker")) 
 | .database.database_config.host = $dgraphip)}' weaviate.conf.json

输出:

{
  "environments": [
    {
      "name": "docker",
      "database": {
        "name": "dgraph",
        "database_config": {
          "host": "1.2.3.4",
          "port": 9080
        }
      },
      "schemas": {
        "Thing": "https://raw.githubusercontent.com/weaviate/weaviate-semantic-schemas/master/weaviate-Thing-ontology-schema_org.min.json",
        "Action": "https://raw.githubusercontent.com/weaviate/weaviate-semantic-schemas/master/weaviate-Action-ontology-schema_org.min.json"
      },
      "mqttEnabled": false
    }
  ]
}
于 2017-10-24T13:31:58.497 回答
1

这是另一种使用地图更新分配的方法|=

.environments |= map(      
     select(.name | contains("docker"))
   | .database.database_config.host = "1.2.3.4"
) 

样品运行(假设数据在data.json

$ jq -M '.environments |= map(select(.name | contains("docker")) | .database.database_config.host = "1.2.3.4")' data.json
{
  "environments": [
    {
      "name": "docker",
      "database": {
        "name": "dgraph",
        "database_config": {
          "host": "1.2.3.4",
          "port": 9080
        }
      },
      "schemas": {
        "Thing": "https://raw.githubusercontent.com/weaviate/weaviate-semantic-schemas/master/weaviate-Thing-ontology-schema_org.min.json",
        "Action": "https://raw.githubusercontent.com/weaviate/weaviate-semantic-schemas/master/weaviate-Action-ontology-schema_org.min.json"
      },
      "mqttEnabled": false
    }
  ]
}

在 jqplay.org 在线尝试

于 2017-10-24T19:07:08.163 回答