英文:
JOLT if else and moving the value of the root field to the nested
问题
我是新手使用Jolt转换。我将尽力详细描述我的情况。
我怀疑您需要结合shift和modify-default操作
这里是转换规则:
如果字段detail具有值**(存在且不为""或null),则将detail映射到updating_field**;
否则:
将updating_field映射到updating_field
输入
{
"updating_field": "ACCEPT",
"channel": {
"detail": null
}
}
期望的输出,例如detail = null值在JOLT内部
{
"channel": {
"updating_field": "ACCEPT"
}
}
其他输入
{
"updating_field": "ACCEPT",
"channel": {
"detail": "normal_value"
}
}
其他期望的输出,例如**detail = "normal_value"**值在JOLT内部
{
"channel": {
"updating_field": "normal_value"
}
}
提前感谢任何帮助。
英文:
I'm new to Jolt transformations. I will try to describe my case in detail.
I suspect that you will need to combine shift and modify-default operations
HERE: TRANSFORMATION RULE:<br>
if field detail have value (exist and is not"" or null)<br>then map detail => updating_field <br>
else: <br>
map updating_field => updating_field
INPUT
{
"updating_field": "ACCEPT",
"channel": {
"detail": null
}
}
EXPECTED OUTPUT for e.g. detail = null value inside JOLT
{
"channel": {
"updating_field": "ACCEPT"
}
}
OTHER INPUT
{
"updating_field": "ACCEPT",
"channel": {
"detail": "normal_value"
}
}
OTHER EXPECTED OUTPUT for e.g. **detail = "normal_value" value inside JOLT
{
"channel": {
"updating_field": "normal_value"
}
}
Thanks in advance for any help.
答案1
得分: 1
你可以在modify-overwrite-beta转换中使用***~
***运算符,例如
"~detail" : "@(2,updating_field)"
意味着如果"detail"
不存在或为空,那么将其设置为在遍历树中的2个级别(:
和{
)后的"updating_field"
的值,如下所示:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"c*": {
"~detail": "@(2,updating_field)"
}
}
},
{ // 将键从"detail"重命名为"updating_field"
"operation": "shift",
"spec": {
"c*": { // "channel"的级别,其中*代表缩写
"*": "&1.updating_field"
}
}
}
]
http://jolt-demo.appspot.com/ 网站上的第一个演示是:
http://jolt-demo.appspot.com/ 网站上的第二个演示是:
编辑:如果将null和空字符串(""
)值分类为相同类别,则可以使用size函数,如下所示:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"c*": {
"sz": ["=size(@(1,detail))", 0]
}
}
},
{
"operation": "shift",
"spec": {
"c*": {
"sz": {
"0": { "@3,updating_field": "&3.updating_field" },
"*": { "@2,detail": "&3.updating_field" } // 否则情况
}
}
}
}
]
英文:
You can use the ~
operator within a modify-overwrite-beta transformation such as
"~detail" : "@(2,updating_field)"
meaning if "detail"
does not exist or is null,
then make it be the value of "updating_field"
after traversing 2 levels (:
and {
) up the tree such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"c*": {
"~detail": "@(2,updating_field)"
}
}
},
{ // rename the key from "detail" to "updating_field"
"operation": "shift",
"spec": {
"c*": { // the level of "channel" where * stand for abbreviation
"*": "&1.updating_field"
}
}
}
]
the first demo on the http://jolt-demo.appspot.com/ site is :
the second demo on the http://jolt-demo.appspot.com/ site is :
Edit : If the null and empty string (""
) values should be classifed as in the same category, then use ths size function as follows :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"c*": {
"sz": ["=size(@(1,detail))", 0]
}
}
},
{
"operation": "shift",
"spec": {
"c*": {
"sz": {
"0": { "@3,updating_field": "&3.updating_field" },
"*": { "@2,detail": "&3.updating_field" } // else case
}
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论