Search code examples
jsonjolt

Struggling with Jolt Mapping v0.1.1


I have been trying to modify a Json using Jolt and no matter what operators I try, I am not able to solve the problem. I'm working on v0.1.1 and using Jolt Transform Demo.

Considering that I should priorize reservation.date, use reservation.reservationList.date in case reservation.date is not provided and in case none of them are provided I should not show any date.

Input:

´{
  "reservation": {
    "date": "2024-03-01",
    "reservationList": [
       "language": "en",
       "date": "2024-03-02"
    ]
  }
  

Spected output:

´{
  "reservation": {
    "date": "2024-03-01",
    "languaje": "en"
  }

Thank you so much in advance if you could enlightme about it.


Solution

  • Hi you can use this spec to get desired output.

    [
      {
        "operation": "shift",
        "spec": {
          "reservation": {
            "reservationList": {
              "*": {
                "@(2,date)": "reservation.date",
                "date": "reservation.date",
                "language": "reservation.language"
              }
            }
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "reservation": {
            "date": "ONE"
          }
        }
      }
    ]
    

    Explanation :

    1. Shift: In first spec I am creating two objects with date field from input.

    Spec 1 output

    1. Cardinality:

    In second spec cardinality date : "ONE" means to extract the first value as a object into date field.

    Here the priority will take place reservation.date is mapped first in previous spec so it will be 1st element of array, if reservation.date is not coming then reservation.reservationList.date will come and in case none of them are provided date wont be present.

    Spec 2