用于根据条件连接值的 Nifi JoltTransformJSON 规范

Nifi JoltTransformJSON spec for concatenating values based on condition

我有 json 表单输入,如下所示。也可以为空,如 []

[
 {
    "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": ""
 }
]

我正在写 NiFi JoltTransformJSON 规范所以我的输出将包含 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" :""
  }
]

基本上它合并了CityProvPostalcode 并放入新密钥地址。它们之间使用 , 逗号连接,并且如果 Postalcode 不为空,则在地址中连接为 Pin:。如果所有内容 (CityProvPostalcode) 都为空,则 Address 将像 "Address":""

一样为空

我正在尝试使用 shift 然后删除

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

我使用 SplitJSON, EvaluateJSON路径UpdateAttribute。但我现在正在尝试使用 JoltTransformJSON。使用 JoltTransformJSON 可以实现吗?

您可以连续使用 modify-overwrite-beta 以及 concatsize 函数和 shift 转换使得

[
  {
    "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].&"  
      }
    }
  }
]

其中size函数用于判断POSTALCODE值是null还是不是.