Home > Blockchain >  JoltTransformJson - Json Transformation
JoltTransformJson - Json Transformation

Time:11-15

i have JSON value as below :

{
  "table": "table_name",
  "op_type": "U",
  "before": {
    "AAAA": "1-1111",
    "BBBB": "2022-08-31 03:57:01",
    "CCCC": "2023-08-31 23:59:59"
  },
  "after": {
    "AAAA": "1-1112",
    "BBBB": "2022-08-31 10:10:34"
  }
}

i want to do this how can i do?

{ 
  "AAAA": "1-1112",
  "BBBB": "2022-08-31 10:10:34",
  "CCCC": "2023-08-31 23:59:59" 
  "changed_columns": "AAAA, BBBB"
}

AAAA: "If you have after.AAAA, take AAAA else before.AAAA", BBBB: "If you have after.BBBB, take BBBB else before.BBBB.

AND I want to add changed_columns field like this :

,"changed_columns": "AAAA, BBBB"

is there a way to do this?

CodePudding user response:

You can use shift operation and getting after values for the first. and then you can using before values. So if keys match with together you have an array with two element.

Now You can get first element to getting after values with modify-overwrite-beta operations and =firstElement function.

[
  {
    "operation": "shift",
    "spec": {
      "after": {
        "*": {
          "$": "changed_columns[]",
          "@(1,&)": "&1"
        }
      },
      "before": {
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=firstElement(@(1,&))",
      "changed_columns": "=join(', ',@(1,changed_columns))"
    }
  }
]

CodePudding user response:

You can use

  • cardinality spec lately after using "after|before" as the key in this order to determine the precedence
  • exchange key-value pairs consecutively twice to determine whether really changed the components in order to form "changed_columns"

such as

[
  {
    // multiplex the attributes in order to generate three independent groups
    "operation": "shift",
    "spec": {
      "after|before": { // this order is important to determine the precedence in the upcoming cardinality spec
        "*": { 
          "@": "&",
          "@(0)": "l.&",
          "*": {
            "@1": "f.&2"
          }
        }
      }
    }
  },
  {
    // determine whether before vs. after values equal through this and next two specs
    "operation": "modify-overwrite-beta",
    "spec": {
      "l": {
        "*": "=lastElement(@(1,&))"
      },
      "f": {
        "*": "=firstElement(@(1,&))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "l|f": {
        "*": {
          "$": "lf.@(0)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "lf": {
        "*": {
          "$": "&2.@(0)"
        }
      }
    }
  },
  {
    // construct an array from those newly formed keys
    "operation": "shift",
    "spec": {
      "*": "&",
      "lf": {
        "*": {
          "$": "changed_columns"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "changed_columns": "=join(', ',@(1,&))"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": "ONE"
    }
  },
  {
    "operation": "sort"
  }
]
  • Related