likeGreen
likeGreen

Reputation: 1079

Nifi JoltTransformJSON spec for concatenating values based on condition

I have json input of form as shown below. It can also be empty like []

[
 {
    "NAME": "Aron",
    "CITY": "NewYork",
    "PROV": "NY",
    "POSTALCODE": "12345",
    "DATE": "2021-08-19",
    "TIME": "14:25:25"
 }, 
 {
    "NAME": "Paul",
    "CITY": "Chicago",
    "PROV": "BI",
    "POSTALCODE": null,
    "DATE": "2021-08-19",
    "TIME": ""
 }
]

I am writing NiFi JoltTransformJSON spec so my output will contain the JSON as

[ 
 {
    "NAME" : "Aron",
    "Address" : "NewYork, NY, Pin: 12345",
    "DATE" : "2021-08-19",
    "TIME" : "14:25:25"
 }, 
 {
    "NAME" : "Paul",
    "Address" : "Chicago, BI",
    "DATE" : "2021-08-19",
    "TIME" :""
  }
]

Basically it merges City, Prov, Postalcode and puts in new key Address. They are concatenated using , comma between them and also if Postalcode is not null it is concatenated in Address as Pin:. If everything (City, Prov, Postalcode) is null then Address will be empty like "Address":""

I am trying with shift followed by remove

[
  {
    "operation": "shift",
    "spec": {
     "@(1,CITY)": "",
      "*": "&"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "CITY": "",
      "PROV": "",
      "POSTALCODE" : "",
    }
  }
]

I have achieved this using SplitJSON, EvaluateJSONPath and UpdateAttribute. But I am trying this using JoltTransformJSON now. Is this achievable using JoltTransformJSON ?

Upvotes: 1

Views: 300

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65373

You can consecutively use modify-overwrite-beta along with concat and size functions and shift transformations such that

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
       "*":{
      "Address1": "=concat(@(1,CITY),',',@(1,PROV))",
      "Address2": "=concat(',Pin:',@(1,POSTALCODE))",
      "Addr2Size": "=size(@(1,Address2))",
      "Address": "=concat(@(1,Address1),@(1,Address2))"
       }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*":{
      "NAME": "[&1].&",                                
      "Addr2Size": {
          "5": {
            "@(2,Address1)": "[&3].Address"
          },
          "*": {
            "@(2,Address)": "[&3].Address"
          }
      },        
      "DATE": "[&1].&",
      "TIME": "[&1].&"  
      }
    }
  }
]

where size function is used to determine whether POSTALCODE value is null or not.

Upvotes: 1

Related Questions