Search code examples
jsonnulldefault-valuejolt

JOLT need missing values to be set as null and keep the same place


I have this 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"
          }
        }
      }
    }
  }
]

and this 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"
      }
    }
  ]
}

This works perfect, but if some of the values is missing, for example: Gender, I need to receive a response with the Gender set as null and in the same place. (I try set a default, null but it places the value at the bottom).

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

Solution

  • What you need is to add a default spec by including all the needed attributes such as

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