繁体   English   中英

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

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

我想转换这个 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
    }
  }
]

期望的输出:

如果“after”为空,则删除“after”并将“before”重命名为“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
    }
  }
]

非常感谢!

在通过使用修改转换确定之前/之后对象/属性的大小之后,您可以在移位转换中使用条件逻辑,在该转换中,我们计算它们各自的大小,无论它们是否返回值。 如果值不返回,则分配-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"
  }
]

那将返回

[
  {
    "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
    }
  }
]

将当前输入作为示例案例。

PS。 :作为问题的缺失案例:如果after / before都是null ,那么两者都在此解决方案中被删除。

编辑:如果输入没有嵌套在方括号内(如您作为额外案例所要求的那样),则将规范转换为这个:

[
  {
    "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