我有一个 json 对象,如下所示
{
"_namespace": "kubernetes",
"api_paths": [
{
"api_action": "post",
"api_path": "v1/ldap",
"api_payload": {
"policies": [
"reader",
"writer",
"encrypt",
"decrypt"
]
}
}
]
}
我期待如下结果
{
"_namespace": "kubernetes",
"api_paths": [
{
"api_action": "post",
"api_path": "v1/ldap",
"api_payload": {
"policies": [
"reader",
"writer",
"encrypt",
"decrypt",
"delete"
]
}
}
]
}
有人可以帮我纠正一下这个表达吗
$ ~> | {
"api_paths": $map(function($path) {
if ($path.api_path["v1/ldap"]) {
return $merge([$path, {
"api_payload": $merge([$path.api_payload, {
"policies": $append(api_payload.policies, "delete")
}])
}])
} else {
return $path
}
})
}
出现错误预期为“:”,得到“合并”
有人可以帮助我了解如何解决这个问题吗?
表达式中存在“If Else”语句,该语句不是有效的 JSONata 语法。
此外, $map 函数缺少第一个参数,该参数应该是函数应迭代的数组。
为了纠正你的表达,我将其重写为:
{
"_namespace": "kubernetes",
"api_paths": $map(
$.api_paths,
function($path) {
$path.api_path = "v1/ldap"
? {
"api_action": $path.api_action,
"api_path": $path.api_path,
"api_payload": { "policies": $append($path.api_payload.policies, "delete") }
}
: $path
}
)
}
但是,我能想到的最佳解决方案是利用 JSONata 转换运算符 (... ~> | ... | ... |),如下所示:
$ ~> |$.api_paths|{ "api_payload": api_path = "v1/ldap" ? $append(api_payload.policies, "delete") }|