1

I started with an input JSON as such.

{
  "trackingNumber": "1ZEA83550362028861",
  "localActivityDate": "20210324",
  "localActivityTime": "183500",
  "scheduledDeliveryDate": "20210324",
  "actualDeliveryDate": "20210324",
  "actualdeliveryTime": "183500",
  "gmtActivityDate": "20210324",
  "gmtActivityTime": "223500",
  "activityStatus": {
    "type": "G",
    "code": "OR",
    "description": "Origin Scan"
  },
  "activityLocation": {
    "city": "RANDALLSTOWN,",
    "stateProvince": "MD",
    "postalCode": "21133",
    "country": "US"
  }
}

This is the JOLT transformation spec that i have written as of now.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "tsY": "=substring(@(1,localActivityDate),0,4)",
      "tsM": "=substring(@(1,localActivityDate),4,6)",
      "tsD": "=substring(@(1,localActivityDate),6,8)",
      "tsH": "=substring(@(1,localActivityTime),0,2)",
      "tsMi": "=substring(@(1,localActivityTime),2,4)",
      "tsS": "=substring(@(1,localActivityTime),4,6)",
      "timeStamp": "=concat(@(1,tsY),'-',@(1,tsM),'-',@(1,tsD),'T',@(1,tsH),':',@(1,tsMi),':',@(1,tsS),'Z')",
      "aTY": "=substring(@(1,scheduledDeliveryDate),0,4)",
      "aTM": "=substring(@(1,scheduledDeliveryDate),4,6)",
      "aTD": "=substring(@(1,scheduledDeliveryDate),6,8)",
      "appointmentTime": "=concat(@(1,aTY),'-',@(1,aTM),'-',@(1,aTD))",
      "dTY": "=substring(@(1,actualDeliveryDate),0,4)",
      "dTM": "=substring(@(1,actualDeliveryDate),4,6)",
      "dTD": "=substring(@(1,actualDeliveryDate),6,8)",
      "dTH": "=substring(@(1,actualdeliveryTime),0,2)",
      "dTMi": "=substring(@(1,actualdeliveryTime),2,4)",
      "dTS": "=substring(@(1,actualdeliveryTime),4,6)",
      "deliveryTime": "=concat(@(1,dTY),'-',@(1,dTM),'-',@(1,dTD),'T',@(1,dTH),':',@(1,dTMi),':',@(1,dTS),'Z')"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*Number": "transformedPayload.&(0,1)Info",
      "activityStatus": {
        "*": "transformedPayload.events.&"
      },
      "activityLocation": {
        "*": "transformedPayload.address.&"
      },
      "timeStamp": "transformedPayload.events[0].&",
      "appointmentTime": "transformedPayload.events[1].&",
      "deliveryTime": "transformedPayload.events[2].&",
      "activityStatus": {
        "type": "transformedPayload.events[0].type",
        "code": "transformedPayload.events[0].statusCode",
        "description": "transformedPayload.events[0].statusDescription"
      },
      "activityLocation": {
        "city": "transformedPayload.address.city",
        "stateProvince": "transformedPayload.address.state",
        "postalCode": "transformedPayload.address.postalCode",
        "country": "transformedPayload.address.country"
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "metaData": {
        "domain": "LTL",
        "eventType": "statusUpdate",
        "version": "v1"
      },
      "transformedPayload": {
        "events": {
          "[1]": {
            "statusCode": "AB",
            "statusDescription": "Delivery Scheduled"
          },
          "[2]": {
            "statusCode": "D1",
            "statusDescription": "Delivered"
          }
        }
      }
    }
  }
]

The resultant JSON created by this transformation looks like this.

{
  "transformedPayload" : {
    "events" : [ {
      "type" : "G",
      "statusCode" : "OR",
      "statusDescription" : "Origin Scan",
      "timeStamp" : "2021-03-24T18:35:00Z"
    }, {
      "appointmentTime" : "2021-03-24",
      "statusCode" : "AB",
      "statusDescription" : "Delivery Scheduled"
    }, {
      "deliveryTime" : "2021-03-24T18:35:00Z",
      "statusCode" : "D1",
      "statusDescription" : "Delivered"
    } ],
    "address" : {
      "city" : "RANDALLSTOWN,",
      "state" : "MD",
      "postalCode" : "21133",
      "country" : "US"
    },
    "trackingInfo" : "1ZEA83550362028861"
  },
  "metaData" : {
    "domain" : "LTL",
    "eventType" : "statusUpdate",
    "version" : "v1"
  }
}

I just need a small tweak in this where the appointmentTime and the deliveryTime fields in the index [1] and [2] of the events array also need to be named as "timestamp" (as seen in the [0]th index). So that finally the correct output JSON looks something like this.

{
  "transformedPayload" : {
    "events" : [ {
      "type" : "G",
      "statusCode" : "OR",
      "statusDescription" : "Origin Scan",
      "timeStamp" : "2021-03-24T18:35:00Z"
    }, {
      "timestamp" : "2021-03-24",
      "statusCode" : "AB",
      "statusDescription" : "Delivery Scheduled"
    }, {
      "timestamp" : "2021-03-24T18:35:00Z",
      "statusCode" : "D1",
      "statusDescription" : "Delivered"
    } ],
    "address" : {
      "city" : "RANDALLSTOWN,",
      "state" : "MD",
      "postalCode" : "21133",
      "country" : "US"
    },
    "trackingInfo" : "1ZEA83550362028861"
  },
  "metaData" : {
    "domain" : "LTL",
    "eventType" : "statusUpdate",
    "version" : "v1"
  }
}

I have tried renaming the field in the shift operation itself but that did not work. I am completely new to JOLT transformation so it seems a bit tricky doing this small change. So any help is appreciated. Thanks

1 Answers1

1

Just convert the related lines within the shift transformation spec from

  "appointmentTime": "transformedPayload.events[1].&",
  "deliveryTime": "transformedPayload.events[2].&",

to

  "appointmentTime": "transformedPayload.events[1].timestamp",
  "deliveryTime": "transformedPayload.events[2].timestamp",

instead of the replicating operator & used in the previous one.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55