英文:
Default JSON property if not exists using Jolt
问题
我想只有在JSON属性不存在时才设置默认值。
例如,下面是我的Jolt规范:
[
{
"operation": "modify-default-beta",
"spec": {
"lastName": "[empty]"
}
},
{
"operation": "shift",
"spec": {
"firstName": "firstName",
"lastName": "lastName"
}
}
]
当输入如下时:
{
"firstName": "John"
}
输出如下所示:
{
"firstName": "John",
"lastName": "[empty]"
}
另一方面,当输入如下时:
{
"firstName": "John",
"lastName": null
}
我期望输出与输入相同:
{
"firstName": "John",
"lastName": null
}
有没有办法只在属性不存在时设置默认值,而不考虑它的值。谢谢。
英文:
I want to set a default value only if the JSON property not exists.
For example, below is my Jolt spec :
[
{
"operation": "modify-default-beta",
"spec": {
"lastName": "[empty]"
}
},
{
"operation": "shift",
"spec": {
"firstName": "firstName",
"lastName": "lastName"
}
}
]
When input is :
{
"firstName": "John"
}
the output is as expected :
{
"firstName" : "John",
"lastName" : "[empty]"
}
On the other hand when input is :
{
"firstName": "John",
"lastName": null
}
I'm expecting the output is same as the input :
{
"firstName": "John",
"lastName": null
}
is there anyway to default only when property not exists regardless of it value. Thanks
答案1
得分: 2
首先,shift 转换是不必要的。您可以在修改转换中使用 isNull 函数,例如
[
{
"operation": "modify-default-beta",
"spec": {
"lastName": ["=isNull(@(1,&))", "[empty]"]
}
}
]
其中
@(1,&)
代表当前属性的值,例如“lastName”
(和符号 代表lastName
文字)- 如果该函数能够运行,则直接返回
null
值,否则返回第二个参数“[empty]”
。
英文:
First of all the shift transformation is not needed. You can use an isNull function within a modify transformation such as
[
{
"operation": "modify-default-beta",
"spec": {
"lastName": ["=isNull(@(1,&))", "[empty]"]
}
}
]
where
@(1,&)
represents the value of the current attribute, eg.
"lastName"
(the ampersand stnds for thelastName
literal)- if the function is able to run, then the value
null
returns directly,
otherwise the second argument"[empty]"
returns
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论