无法按预期填充字段 在 jolt 变换中,如 if-else 和 if-else(嵌套)?

问题描述 投票:0回答:1

JSON 输入

{
  "code": "+91-",
  "status": "A",
  "number": "123456778",
  "country": "I",
  "pukcode": "00-766-54"
}

我的 Jolt Spec 1 :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=trim",
      "mobile": "=concat(@(1,code),@(1,number),"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "eventInfo": "eventInfo",
      "mobile": {
        "": {
          "comment": [
            "if blank, do not pass mobile"
          ]
        },
        "*": {
          "@(2,mobile)": "eventData.Records.[0].mobile",
          "@(2,status)": {
            "A": {
              "#Active": "eventData.Records.[0].status"
            }
          },
          "@(2,country)": {
            "I": {
              "#India": "eventData.Records.[0].country"
            }
          },
          "@(2,pukcode)": {
            "": {
              "comment": [
                "if blank, do not pass mobile"
              ]
            },
            "*": {
              "@(2,pukcode)": "eventData.Records.[0].pukcode"
            }
          }
        }
      }
    }
  }
]

输出

{
  "eventData" : {
    "Records" : [ {
      "mobile" : "+91-123456778",
      "status" : "Active",
      "country" : "India"
    } ]
  }
}

对于上面的输入,我需要根据手机号码的存在填充数据,然后只有它应该填充所有字段,并且当输入数据存在于移动目标的输入中时,puk 代码也应该填充。但这里的问题是,即使字段包含输入数据,pukcode 也不会填充。

我的 Jolt Spec 2 :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=trim",
      "mobile": "=concat(@(1,code),@(1,number),"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "eventInfo": "eventInfo",
      "mobile": {
        "": {
          "comment": [
            "if blank, do not pass mobile"
          ]
        },
        "*": {
          "@(2,mobile)": "eventData.Records.[0].mobile",
          "@(2,status)": {
            "A": {
              "#Active": "eventData.Records.[0].status"
            }
          },
          "@(2,country)": {
            "I": {
              "#India": "eventData.Records.[0].country"
            }
          },
          "@(2,pukcode)": {
            "": {
              "comment": [
                "if blank, do not pass mobile"
              ]
            },
            "*": "eventData.Records.[0].pukcode"
          }
        }
      }
    }
  }
]

输出2

{
  "eventData": {
    "Records": [
      {
        "mobile": "+91-123456778",
        "status": "Active",
        "country": "India",
        "pukcode": null
      }
    ]
  }
}

请提供上述问题的一些解决方案

案例1 JSON 输入 :

{
  "code": "+91-",
  "status": "A",
  "number": "123456778",
  "country": "I",
  "pukcode": "00-766-54"
}

预期输出 1 :

{
  "eventData": {
    "Records": [
      {
        "mobile": "+91-123456778",
        "status": "Active",
        "country": "India",
        "pukcode": "00-766-54"
      }
    ]
  }
}

案例 2 JSON 输入 :

{
  "code": "",
  "status": "A",
  "number": "",
  "country": "I",
  "pukcode": "00-766-54"
}

预期输出 2 :

null
json cron apache-nifi nested-loops jolt
1个回答
0
投票

您可以使用以下转换规范:

[
  {
    "operation": "shift",
    "spec": {
      "number": {
        "*": {
          "@2,code|@1": "mobile",
          "@2,status": { "A": { "#Active": "status" } }, // renaming occurs here
          "@2,country": { "I": { "#India": "country" } },// and here
          "@2,pukcode": "pukcode"
        },
        "": { // if "number" is null then remove every element
          "": ""
        }
      }
    }
  },
  { // concatenate the components of mobile array if it exists
    "operation": "modify-overwrite-beta",
    "spec": {
      "mobile": "=join('',@(1,&))"
    }
  },
  { // nest the current object along with some keys as desired
    "operation": "shift",
    "spec": {
      "*": "eventData.Records[0].&"
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.