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

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

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

输入

  1. {
  2. "updating_field": "ACCEPT",
  3. "channel": {
  4. "detail": null
  5. }
  6. }

期望的输出,例如detail = null值在JOLT内部

  1. {
  2. "channel": {
  3. "updating_field": "ACCEPT"
  4. }
  5. }

其他输入

  1. {
  2. "updating_field": "ACCEPT",
  3. "channel": {
  4. "detail": "normal_value"
  5. }
  6. }

其他期望的输出,例如**detail = "normal_value"**值在JOLT内部

  1. {
  2. "channel": {
  3. "updating_field": "normal_value"
  4. }
  5. }

提前感谢任何帮助。

英文:

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

  1. {
  2. &quot;updating_field&quot;: &quot;ACCEPT&quot;,
  3. &quot;channel&quot;: {
  4. &quot;detail&quot;: null
  5. }
  6. }

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

  1. {
  2. &quot;channel&quot;: {
  3. &quot;updating_field&quot;: &quot;ACCEPT&quot;
  4. }
  5. }

OTHER INPUT

  1. {
  2. &quot;updating_field&quot;: &quot;ACCEPT&quot;,
  3. &quot;channel&quot;: {
  4. &quot;detail&quot;: &quot;normal_value&quot;
  5. }
  6. }

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

  1. {
  2. &quot;channel&quot;: {
  3. &quot;updating_field&quot;: &quot;normal_value&quot;
  4. }
  5. }

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;的值,如下所示:

  1. [
  2. {
  3. &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
  4. &quot;spec&quot;: {
  5. &quot;c*&quot;: {
  6. &quot;~detail&quot;: &quot;@(2,updating_field)&quot;
  7. }
  8. }
  9. },
  10. { // 将键从&quot;detail&quot;重命名为&quot;updating_field&quot;
  11. &quot;operation&quot;: &quot;shift&quot;,
  12. &quot;spec&quot;: {
  13. &quot;c*&quot;: { // &quot;channel&quot;的级别,其中*代表缩写
  14. &quot;*&quot;: &quot;&amp;1.updating_field&quot;
  15. }
  16. }
  17. }
  18. ]

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

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

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

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

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

  1. [
  2. {
  3. &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
  4. &quot;spec&quot;: {
  5. &quot;c*&quot;: {
  6. &quot;sz&quot;: [&quot;=size(@(1,detail))&quot;, 0]
  7. }
  8. }
  9. },
  10. {
  11. &quot;operation&quot;: &quot;shift&quot;,
  12. &quot;spec&quot;: {
  13. &quot;c*&quot;: {
  14. &quot;sz&quot;: {
  15. &quot;0&quot;: { &quot;@3,updating_field&quot;: &quot;&amp;3.updating_field&quot; },
  16. &quot;*&quot;: { &quot;@2,detail&quot;: &quot;&amp;3.updating_field&quot; } // 否则情况
  17. }
  18. }
  19. }
  20. }
  21. ]
英文:

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

  1. [
  2. {
  3. &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
  4. &quot;spec&quot;: {
  5. &quot;c*&quot;: {
  6. &quot;~detail&quot;: &quot;@(2,updating_field)&quot;
  7. }
  8. }
  9. },
  10. { // rename the key from &quot;detail&quot; to &quot;updating_field&quot;
  11. &quot;operation&quot;: &quot;shift&quot;,
  12. &quot;spec&quot;: {
  13. &quot;c*&quot;: { // the level of &quot;channel&quot; where * stand for abbreviation
  14. &quot;*&quot;: &quot;&amp;1.updating_field&quot;
  15. }
  16. }
  17. }
  18. ]

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 :

  1. [
  2. {
  3. &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
  4. &quot;spec&quot;: {
  5. &quot;c*&quot;: {
  6. &quot;sz&quot;: [&quot;=size(@(1,detail))&quot;, 0]
  7. }
  8. }
  9. },
  10. {
  11. &quot;operation&quot;: &quot;shift&quot;,
  12. &quot;spec&quot;: {
  13. &quot;c*&quot;: {
  14. &quot;sz&quot;: {
  15. &quot;0&quot;: { &quot;@3,updating_field&quot;: &quot;&amp;3.updating_field&quot; },
  16. &quot;*&quot;: { &quot;@2,detail&quot;: &quot;&amp;3.updating_field&quot; } // else case
  17. }
  18. }
  19. }
  20. }
  21. ]

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:

确定