我正在跑步:
$ 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
}]
}
答案 0 :(得分: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
}
]
}
答案 1 :(得分:1)
以下是另一种使用update assignment |=
与map
.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
}
]
}