Search code examples
apache-nifijolt

JoltTransformJSON - 3 JSON transformations


I am working with the JoltTransformJSON processor, which I consider to be very powerful but complicated to use and there is not much documentation about it.

I have the following input JSON:

{
   "logTypeCode":"FIN",
   "eventNumber":"1",
   "eventTime":1568066703172,
   "eventInfo":{
      "eventId":"CREATE_DOMESTIC_STANDING_ORDER",
      "eventVersion":"1"
   },
   "operationInfo":{
      "operationId":"DOMESTIC_STANDING_ORDER"
   }
}

And my my desirable output JSON:

{
   "body":{
      "logTypeCode":"END_OF_TESTING",
      "eventNumber":"1",
      "eventTime":1568066703172,
      "eventInfo":{
         "eventId":"CREATE_DOMESTIC_STANDING_ORDER",
         "eventVersion":"1"
      },
      "operationInfo":{
         "operationId":"DOMESTIC_STANDING_ORDER"
      }
   },
   "header":{
      "date":"${dateInPropertiesFlow}"
   },
   "customValue":{
      "logTypeCode":"END_OF_TESTING",
      "ENV":"${anotherPropertie}"
   }
}

basically, I want to group everything that comes to me inside the "body" field, then I need to create another field that is called "header", which contains a single field called "date" with the value of a property that comes to me by the flow, and finally I need a field that is called "customValue", where the value "logTypeCode" has to have the same value as the field that is in "body.logTypeCode".

I'm trying several ways in the application https://jolt-demo.appspot.com/#inception but I can't get it to work as I want, for now this is the closest I got....

[
  {
    "operation": "shift",
    "spec": {
      "*": "body.&"
    }
   },
  {
    "operation": "default",
    "spec": {
      "header": {
        "date": "${paramDate}"
      }
    }
  }
]

I've been doing a lot of tests, but I can't get the result I need... Help !


Solution

  • You can start and end the specification by modify-overwrite-beta operations such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "logTypeCode": "END_OF_TESTING"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "body.&"
        }
      },
      {
        "operation": "default",
        "spec": {
          "header": {
            "date": "${dateInPropertiesFlow}"
          },
          "customValue": {
            "logTypeCode": "logTypeCode",
            "ENV": "${anotherPropertie}"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "customValue": {
            "logTypeCode": "@(2,body.logTypeCode)"
          }
        }
      }
    ]