[英]jq 1.5 - Change existing element or add new if does not exist
unique
这样的函数 结构实际上是强加的,所以我必须服从它。 对象“路径”通常类似于: .spec.template.spec.containers[0].spec.env[1].name
。 你也可以有 .containers[1],等等。 这是高度可变的,有时某些元素可能存在与否,取决于该特定 JSON 的模式定义。
[
{
"kind": "StatefulSet",
"spec": {
"serviceName": "cassandra",
"template": {
"spec": {
"containers": [
{
"name": "cassandra",
"env": [
{
"name": "CASSANDRA_SEEDS",
"value": "cassandra-0.cassandra.kong.svc.cluster.local"
},
{
"name": "CHANGEME",
"value": "K8"
}
]
}
]
}
}
}
}
]
jq -r 'map({name:"CHANGEME",value: "xx"} as $v | (.spec.template.spec.containers[].env[] | select(.name==$v.name))|=$v)'
让我们假设我想做同样的事情,只有 .env1 是对象 {name:"",value:""} 的父数组。 预期的输出应该是:
[ { "kind": "StatefulSet", "spec": { "serviceName": "cassandra", "template": { "spec": { "containers": [ { "name": "cassandra", "env": [ { "name": "CASSANDRA_SEEDS", "value": "cassandra-0.cassandra.kong.svc.cluster.local" }, { "name": "CHANGEME", "value": "K8" } ], "env1": [ { "name": "CHANGEME", "value": "xx" } ] } ] } } } } ]
jq -r 'map({name:"CHANGEME",value: "xx"} as $v | (.spec.template.spec.containers[] | if .env1 == null then .+={env1:[$v]} | .env1 else .env1 end | .[] | select(.name==$v.name))|=$v)'
.env//[$v]
或.env//=.env[$v]
等符号,结果相同jq -r 'map({name:"CHANGEME",value: "xx"} as $v | (.spec.template.spec.containers[].env1 | .[if length<0 then 0 else length end]) |= $v)'
jq -r 'def defarr: if length<=0 then .[0] else .[] end; def defarr(item): if length<=0 then .[0] else foreach .[] as $item ([]; if $item.name == item then $item else empty end; .) end; map({name:"CHANGEME",value: "xx"} as $v | (.spec.template.spec | .containers1 | defarr | .env1 | defarr($v.name) ) |=$v)'
有没有办法简化这一切,让它更通用一点来处理任意数量的父级、数组?
谢谢你。
回答这个问题:是的。 jq 1.5 具有keys_unsorted
,因此您可以使用walk/1
的以下定义,它现在是 jq 的“主”版本中的标准:
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys_unsorted[] as $key
( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
更多细节和示例,请参见 jq 手册的“开发”版本,jq FAQ https://github.com/stedolan/jq/wiki/FAQ等。
这可以使用index/1
轻松完成; 您可能喜欢使用辅助函数,例如:
def ensure_has($x): if index([$x]) then . else . + [$x] end;
如果我正确理解了这个要求,那么知道 jq 将根据分配创建对象对您很有用,例如
{} | .a.b.c = 1
产量
{"a":{"b":{"c":1}}}
因此,使用您的示例,您可能希望在您的walk
包含这样的内容:
if type == "object" and has("spec")
then (.spec.template.spec.containers? // null) as $existing
| if $existing then .spec.template.spec.containers |= ...
else .spec.template.spec.containers = ...
end
else .
end
设法达到一个非常好的形式:
在~/.jq
添加了以下功能:
def arr: if length<=0 then .[0] else .[] end; def arr(f): if length<=0 then .[0] else .[]|select(f) end//.[length]; def when(COND; ACTION): if COND? // null then ACTION else . end; # Apply f to composite entities recursively, and to atoms def walk(f): . as $in | if type == "object" then reduce keys_unsorted[] as $key ( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f elif type == "array" then map( walk(f) ) | f else f end; def updobj(f): walk(when(type=="object"; f));
典型的过滤器如下所示:
jq -r '{name:"CHANGEME",value: "xx"} as $v | map( when(.kind == "StatefulSet"; .spec.template.spec.containers|arr|.env|arr(.name==$v.name)) |= $v)'
结果将是所有不存在的对象都将被创建。 这里的约定是对每个要成为数组的对象使用arr
函数,最后使用布尔条件和对象来替换匹配的对象或添加到父数组(如果不匹配)。
如果您知道路径始终存在,并且要更新的对象也是如此,则walk
更优雅:
jq -r 'map(updobj(select(.name=="CHANGEME").value|="xx"))'
感谢@peak 的努力和启发解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.