简体   繁体   中英

JOLT Transformation Rename & Remove based on null value

I want to transform this 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"

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

[
  {
    "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. : as a missing case for the question : if both after / before are null , then both are removed within this solution.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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