简体   繁体   English

基于空值的 JOLT 转换重命名和删除

[英]JOLT Transformation Rename & Remove based on null value

I want to transform this JSON:我想转换这个 JSON:

[
  {
    "payload": {
      "before": {
        "order_id": 3110445250,
        "data": "2022-06-10",
        "ora": "08:08:00",
        "test1": "2022-06-10T00:00:00.000Z",
        "test2": "2022-06-10T03:00:00.000Z"
      },
      "after": null,
      "source": {
        "file": "delta.000002",
        "pos": 8362,
        "row": 0,
        "thread": null,
        "query": null
      },
      "op": "c",
      "ts_ms": 1654851760103,
      "transaction": null
    }
  }
]

Desired output:期望的输出:

if "after" is null then remove "after" and rename "before" to "final"如果“after”为空,则删除“after”并将“before”重命名为“final”

else if "before" is null then remove "before" and rename after to "final"否则,如果“之前”为空,则删除“之前”并将之后重命名为“最终”

else if "after" is not null and "before" is not null then remove "before" and rename after to "final"否则,如果“之后”不为空且“之前”不为空,则删除“之前”并将之后重命名为“最终”

[
  {
    "payload": {
      "before": {
        "order_id": 3110445250,
        "data": "2022-06-10",
        "ora": "08:08:00",
        "test1": "2022-06-10T00:00:00.000Z",
        "test2": "2022-06-10T03:00:00.000Z"
      },
      "source": {
        "file": "delta.000002",
        "pos": 8362,
        "row": 0,
        "thread": null,
        "query": null
      },
      "op": "c",
      "ts_ms": 1654851760103,
      "transaction": null
    }
  }
]

Thanks a lot!非常感谢!

You can use a conditional logic within a shift transformation after determining the size s of before/after objects/attributes through use of a modify transformation in which we compute their respective sizes whether they return a value.在通过使用修改转换确定之前/之后对象/属性的大小之后,您可以在移位转换中使用条件逻辑,在该转换中,我们计算它们各自的大小,无论它们是否返回值。 In cases the value doesn't return, then -1 is assigned to determine the element(object or attribute) to be null such as如果值不返回,则分配-1以确定元素(对象或属性)为,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "payload": {
          "before_sz": ["=size(@(1,before))", -1],
          "after_sz": ["=size(@(1,after))", -1]
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "payload": {
          "before_sz": {
            "-1": {
              "@(2,after_sz)": {
                "-1": { "": "" },
                "*": { "@(4,after)": "[&6].&5.final" }
              }
            },
            "*": {
              "@(2,after_sz)": {
                "-1": { "@(4,before)": "[&6].&5.final" },
                "*": { "@(4,after)": "[&6].&5.final" }
              }
            }
          },
          "*": "[&2].&1.&"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "payload": {
          "after_sz": "",
          "after": "",
          "before_sz": "",
          "before": ""
        }
      }
    }
  },
  {
    "operation": "sort"
  }
]

that will return那将返回

[
  {
    "payload": {
      "final": {
        "data": "2022-06-10",
        "ora": "08:08:00",
        "order_id": 3110445250,
        "test1": "2022-06-10T00:00:00.000Z",
        "test2": "2022-06-10T03:00:00.000Z"
      },
      "op": "c",
      "source": {
        "file": "delta.000002",
        "pos": 8362,
        "query": null,
        "row": 0,
        "thread": null
      },
      "transaction": null,
      "ts_ms": 1654851760103
    }
  }
]

for the current input as a sample case.将当前输入作为示例案例。

PS. PS。 : as a missing case for the question : if both after / before are null , then both are removed within this solution. :作为问题的缺失案例:如果after / before都是null ,那么两者都在此解决方案中被删除。

Edit : If the input is not nested within square brackets( as you asked as an extra case ), then convert the spec to this one :编辑:如果输入没有嵌套在方括号内(如您作为额外案例所要求的那样),则将规范转换为这个:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "payload": {
        "before_sz": ["=size(@(1,before))", -1],
        "after_sz": ["=size(@(1,after))", -1]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "payload": {
        "before_sz": {
          "-1": {
            "@(2,after_sz)": {
              "-1": { "": "" },
              "*": { "@(4,after)": "&5.final" }
            }
          },
          "*": {
            "@(2,after_sz)": {
              "-1": { "@(4,before)": "&5.final" },
              "*": { "@(4,after)": "&5.final" }
            }
          }
        },
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "payload": {
        "after_sz": "",
        "after": "",
        "before_sz": "",
        "before": ""
      }
    }
  },
  {
    "operation": "sort"
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM