我有这个Jolt spec:

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

而这input人:

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

这可以完美地工作,但如果缺少一些值,例如:Gender,我需要在相同的位置收到性别设置为空的响应.(我try 将缺省值设置为NULL,但它会将值放在底部).

example input:

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

desired output:

[
  {
    "id": "124354",
    "Test_code": "123454",
    "Email": "blabla@gmail.com",
    "PermanentAddress": null,
    "Gender": null,
    "Date": "2024-01-28 23:23:39",
    "Industry": "IT-Software",
    "StartDate": "2013-04-01",
    "TotalExperience": "11"
  }
]

推荐答案

您需要的是添加一个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].&"
      }
    }
  }
]

Json相关问答推荐

为什么terraform不缩小这个策略JSON?'

写入JSON文件的流

当 JSON 字段名称有空格时,ABAP 中的 JSON 反序列化

JOLT分裂和数组数据

Rust实现:高效解析任意大小的JSON数组

使用 TypeScript 接口时如何修复未定义错误?

如何将该 JSON 解析为 Kotlin 类?

Powershell 7.2:ConvertFrom-Json - 日期处理

如何在循环中传递列表(会话(字符串属性))以在 Gatling Scala 中创建批量 Json 请求

Flutter:在本地文件 json 中搜索特殊的阿拉伯字符

如何使用 gson 将数据保存在 json 文件中?

获取json中某个键的索引

在 C# 中解析 Json rest api 响应

杰克逊:反序列化 for each 值都具有正确类型的 Map

有没有办法在 angular.json 中扩展配置?

使用 application/json 优于 text/plain 的优势?

将 JsonArray 添加到 JsonObject

如何对 Javascript 对象进行排序,或将其转换为数组?

在没有 ASP.NET Core 的情况下将 IConfigurationSection 绑定到复杂对象

play 2 JSON 格式中缺少的属性的默认值