skip to Main Content

Does anyone how to write a JOLT Transform with this situation:

Input :

[
  {
    "id": "65cf06b62fadc0122dc30f29",
    "code": "H34.23.9-240216-0003",
    "applicant": {
      "eformId": "60d990f88e6893001e5a7b42",
      "userId": "6186433deee4513fe0d86efc",
      "data": {
        "birthday": "",
        "address": "Thôn Kon Năng Treang",
        "gender": 2,
        "nation": {
          "label": "Việt Nam",
          "value": "5f39f4a95224cf235e134c5c"
        },
        "ghiChu": "",
        "identityDate": "",
        "noiDung": "",
        "phoneNumber": "",
        "province": {
          "label": "Tỉnh Kon Tum",
          "value": "5def47c5f47614018c000062"
        },
        "identityNumber": "062195002130",
        "soBoHoSo": "1",
        "organization": "Y Thảo",
        "district": {
          "label": "Huyện Đắk Hà",
          "value": "5def47c5f47614018c001615"
        },
        "identityAgency": {},
        "fullname": "Y Thảo",
        "fax": "",
        "village": {
          "label": "Xã Đắk Ui",
          "value": "5def47c5f47614018c123509"
        },
        "email": ""
      }
    }
  },
  {
    "id": "65cf24e3f21075472fe549c7",
    "code": "H34.6-240216-0032",
    "nationCode": "G22.99-240216-030023-H34",
    "applicant": {
      "eformId": "624bb547cdb7f7ec092bee16",
      "data": {
        "fullname": "CÔNG TY CỔ PHẦN VIEON",
        "province": {
          "label": "Thành phố Hồ Chí Minh",
          "value": "5def47c5f47614018c000079"
        },
        "district": {
          "label": "Quận 3",
          "value": "5def47c5f47614018c001770"
        },
        "village": {
          "label": "Phường Võ Thị Sáu",
          "value": "5def47c5f47614018c127139"
        },
        "phoneNumber": "02838241919",
        "fax": "02838241919",
        "mail": "[email protected]",
        "taxCode": "0314415573",
        "contact": "Hoàng Lê Huế An ",
        "contactPhoneNumber": "0385792805",
        "chonDoiTuong": 2,
        "hinhThucNop": 2,
        "ownerFullname": "CÔNG TY CỔ PHẦN VIEON",
        "identityNumber": "0314415573",
        "ownerIdentityNumber": "0314415573",
        "isOwnerDossier": true
      }
    }
  }
]

Expected output :

[
  {
    "id": "65cf06b62fadc0122dc30f29",
    "eformId": "60d990f88e6893001e5a7b42",
    "gender": 2,
    "fullname": "Y Thảo",
    "ownerFullname": "",
    "province_label": "Tỉnh Kon Tum"
  },
  {
    "id": "65cf24e3f21075472fe549c7",
    "eformId": "624bb547cdb7f7ec092bee16",
    "gender": null,
    "fullname": "CÔNG TY CỔ PHẦN VIEON",
    "ownerFullname": "CÔNG TY CỔ PHẦN VIEON",
    "province_label": "Thành phố Hồ Chí Minh"
  }
]

2

Answers


  1. You should loop through all the objects within the array in a shift spec such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[&1].&",
            "@applicant.eformId": "[&1].eformId",
            "@applicant.data.gender": "[&1].gender",
            "@applicant.data.fullname": "[&1].fullname",
            "@applicant.data.ownerFullname": "[&1].ownerFullname",
            "@applicant.data.province.label": "[&1].province_label"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "~ownerFullname": ""
          }
        }
      }
    ]
    

    and add a modify spec in order to add ownerFullname attributes, with null values, if they don’t exist for any object

    Login or Signup to reply.
  2. Before sharing the answer, I would like to remind you about publishing data from your application to the internet, I can see there are phone numbers, addresses, and emails from your users/customers on the JSON data. That might lead you to the unnecessary issue of data privacy.


    Here is the way you can approach your decided JSON output:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[&1].&",
            "@applicant.eformId": "[&1].eformId",
            "@applicant.data.gender": "[&1].gender",
            "@applicant.data.fullname": "[&1].fullname",
            "@applicant.data.ownerFullname": "[&1].ownerFullname",
            "@applicant.data.province.label": "[&1].province_label"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "~ownerFullname": ""
          }
        }
      },
      {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "~gender": null // if "key2" does not exist or is null, then default it to be null
          }
        }
      }
    ]
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search