if else以及将根字段的值移动到嵌套中

huangapple go评论87阅读模式
英文:

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

{
  &quot;updating_field&quot;: &quot;ACCEPT&quot;,
  &quot;channel&quot;: {
    &quot;detail&quot;: null
  }
}

EXPECTED OUTPUT for e.g. detail = null value inside JOLT

{
  &quot;channel&quot;: {
    &quot;updating_field&quot;: &quot;ACCEPT&quot;
  }
}

OTHER INPUT

{
  &quot;updating_field&quot;: &quot;ACCEPT&quot;,
  &quot;channel&quot;: {
    &quot;detail&quot;: &quot;normal_value&quot;
  }
}

OTHER EXPECTED OUTPUT for e.g. **detail = "normal_value" value inside JOLT

{
  &quot;channel&quot;: {
    &quot;updating_field&quot;: &quot;normal_value&quot;
  }
}

Thanks in advance for any help.

答案1

得分: 1

你可以在modify-overwrite-beta转换中使用***~***运算符,例如

&quot;~detail&quot; : &quot;@(2,updating_field)&quot; 意味着如果&quot;detail&quot;不存在或为空,那么将其设置为在遍历树中的2个级别(:{)后的&quot;updating_field&quot;的值,如下所示:

[
  {
    &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: {
        &quot;~detail&quot;: &quot;@(2,updating_field)&quot;
      }
    }
  },
  { // 将键从&quot;detail&quot;重命名为&quot;updating_field&quot;
    &quot;operation&quot;: &quot;shift&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: { // &quot;channel&quot;的级别,其中*代表缩写
        &quot;*&quot;: &quot;&amp;1.updating_field&quot;
      }
    }
  }
]

http://jolt-demo.appspot.com/ 网站上的第一个演示是:

if else以及将根字段的值移动到嵌套中

http://jolt-demo.appspot.com/ 网站上的第二个演示是:

if else以及将根字段的值移动到嵌套中

编辑:如果将null和空字符串(&quot;&quot;)值分类为相同类别,则可以使用size函数,如下所示:

[
  {
    &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: {
        &quot;sz&quot;: [&quot;=size(@(1,detail))&quot;, 0]
      }
    }
  },
  {
    &quot;operation&quot;: &quot;shift&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: {
        &quot;sz&quot;: {
          &quot;0&quot;: { &quot;@3,updating_field&quot;: &quot;&amp;3.updating_field&quot; },
          &quot;*&quot;: { &quot;@2,detail&quot;: &quot;&amp;3.updating_field&quot; } // 否则情况
        }
      }
    }
  }
]
英文:

You can use the ~ operator within a modify-overwrite-beta transformation such as

&quot;~detail&quot; : &quot;@(2,updating_field)&quot; meaning if &quot;detail&quot; does not exist or is null,

then make it be the value of &quot;updating_field&quot; after traversing 2 levels (: and {) up the tree such as

[
  {
    &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: {
        &quot;~detail&quot;: &quot;@(2,updating_field)&quot;
      }
    }
  },
  { // rename the key from &quot;detail&quot; to &quot;updating_field&quot;
    &quot;operation&quot;: &quot;shift&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: { // the level of &quot;channel&quot; where * stand for abbreviation
        &quot;*&quot;: &quot;&amp;1.updating_field&quot;
      }
    }
  }
]

the first demo on the http://jolt-demo.appspot.com/ site is :

if else以及将根字段的值移动到嵌套中

the second demo on the http://jolt-demo.appspot.com/ site is :

if else以及将根字段的值移动到嵌套中

Edit : If the null and empty string (&quot;&quot;) values should be classifed as in the same category, then use ths size function as follows :

[
  {
    &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: {
        &quot;sz&quot;: [&quot;=size(@(1,detail))&quot;, 0]
      }
    }
  },
  {
    &quot;operation&quot;: &quot;shift&quot;,
    &quot;spec&quot;: {
      &quot;c*&quot;: {
        &quot;sz&quot;: {
          &quot;0&quot;: { &quot;@3,updating_field&quot;: &quot;&amp;3.updating_field&quot; },
          &quot;*&quot;: { &quot;@2,detail&quot;: &quot;&amp;3.updating_field&quot; } // else case
        }
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年7月31日 19:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定