JOLT 需要将缺失值设置为 null 并保持相同位置

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

我有这个震动

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "id": "[&1].id",
          "Test_code": "[&1].Test_code",
          "applyData": {
            "Email": "[&2].Email",
            "Address": "[&2].Address",
            "Gender": "[&2].Gender",
            "Date": "[&2].Date",
            "Industry": "[&2].Industry",
            "StartDate": "[&2].StartDate",
            "TotalExperience": "[&2].TotalExperience"
          }
        }
      }
    }
  }
]

和这个输入

{
  "data": [
    {
      "id": "124354",
      "applicantId": "10775119",
      "Test_code": "123454",
      "applyData": {
        "Email": "[email protected]",
        "Address": null,
        "Gender": "M",
        "Date": "2024-01-28 23:23:39",
        "Industry": "IT-Software",
        "StartDate": "2013-04-01",
        "TotalExperience": "11"
      }
    }
  ]
}

这工作完美,但如果缺少某些值,例如:性别,我需要收到将性别设置为 null 并在同一位置的响应。 (我尝试设置默认值 null,但它将值放在底部)。

输入示例

{
  "data": [
    {
      "id": "124354",
      "applicantId": "10775119",
      "Test_code": "123454",
      "applyData": {
        "Email": "[email protected]",
        "Address": null,
        "Date": "2024-01-28 23:23:39",
        "Industry": "IT-Software",
        "StartDate": "2013-04-01",
        "TotalExperience": "11"
      }
    }
  ]
}

所需输出

[ {
  "id" : "124354",
  "Test_code" : "123454",
  "Email" : "[email protected]",
  "PermanentAddress" : null,
  "Gender" : null,
  "Date" : "2024-01-28 23:23:39",
  "Industry" : "IT-Software",
  "StartDate" : "2013-04-01",
  "TotalExperience" : "11"
} ]
null default-value jolt
1个回答
0
投票

您需要的是添加一个 default 规范,包括所有需要的属性,例如

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "id|Test_code": "[&1].&",
          "applyData": {
            "*": "[&2].&",
            "Address": "[&2].Permanent&"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "id": null,
        "Test_code": null,
        "Email": null,
        "PermanentAddress": null,
        "Gender": null,
        "Date": null,
        "Industry": null,
        "StartDate": null,
        "TotalExperience": null
      }
    }
  },
  { // this spec is added only for sorting the attributes
    "operation": "shift",
    "spec": {
      "*": {
        "id|Test_code|Email|PermanentAddress|Gender|Date|Industry|StartDate|TotalExperience": "[&1].&"
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.