如何在 jolt 中添加 null 条件检查?

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

我是震动映射新手。以下是输入和预期输出。

场景1:

输入:

{
  "name": "Fred",
  "phone": [
    {
      "mobile": "1112223333"
    }
  ]
}

预期输出:

{
  "name" : "Fred",
  "customer" : {
    "mobile" : "1112223333"
  }
}

场景2: 输入:

{
  "name": "Fred",
  "phone": null
}

预期输出:

{
  "name" : "Fred",
  "customer" : {
    "mobile" : "000"
  }
}

我尝试了以下规格。场景 1 按预期工作,但是场景 2 失败并且不读取空检查。

规格:

[
  {
    "operation": "shift",
    "spec": {
      "name": "name",
      "phone": {
        "null": {
          "#000": "customer.mobile"
        },
        "*": {
          "mobile": "customer.mobile"
        }
      }
    }
  }
]

我添加了一个空条件,如果“customer.mobile”为空,则默认为 000,否则使用现有的输入值。感谢任何帮助,谢谢!

json transformation jolt
1个回答
0
投票

您可以使用 ~ 符号来检查 modify 规范中的 nullityexistence,例如:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "phone": {
        "~mobile": "000" // means if "phone" does not exist or is null, then make it be "000"
      }
    }
  },
  {//in order to resemble the cases each other
    "operation": "modify-overwrite-beta",
    "spec": {
      "phone": "=toList"
    }
  },
  {//return the result as desired
    "operation": "shift",
    "spec": {
      "*": "&",
      "phone": {
        "*": {
          "mobile": "customer.&"
        }
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.