Search code examples
javajsonjolt

Jolt Transformation JSON array of array issue


I am new to Jolt and trying to transform a JSON which has got an array of array objects. What I noticed is child array elements ("times") of one parent are mixing up with child array from another parent. Please let me know how to fix this?

Input JSON

[
  {
    "id": "1",
    "name": "data1",
    "opening": [
      {
        "From": "07:30:01",
        "To": "17:30:01"
      },
      {
        "From": "07:30:02",
        "To": "17:30:02"
      }
    ]
  },
  {
    "id": "2",
    "name": "data2",
    "opening": [
      {
        "From": "07:30:03",
        "To": "17:30:03"
      },
      {
        "From": "07:30:04",
        "To": "17:30:04"
      }
    ]
  }
]

Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].id",
        "name": "[&1].name",
        "opening": {
          "*": {
            "From": "[&1].timing.[&3].times[0].from",
            "To": "[&1].timing.[&3].times[0].to"
          }
        }
      }
    }
  }
]

Expected output

[ {
  "id" : "1",
  "name" : "data1",
  "timing" : [ {
    "times" : [ {
      "from" : "07:30:01",
      "to" : "17:30:01"
    } ]
  }, {
    "times" : [ {
      "from" : "07:30:02",
      "to" : "17:30:02"
    } ]
  } ]
}, {
  "timing" : [ {
    "times" : [ {
      "from" : "07:30:03",
      "to" : "17:30:03"
    } ]
  }, {
    "times" : [ {
      "from" : "07:30:04",
      "to" : "17:30:04"
    } ]
  } ],
  "id" : "2",
  "name" : "data2"
} ]

Current ouput

[ {
  "id" : "1",
  "name" : "data1",
  "timing" : [ {
    "times" : [ {
      "from" : "07:30:01",
      "to" : "17:30:01"
    } ]
  }, {
    "times" : [ {
      "from" : "07:30:03",
      "to" : "17:30:03"
    } ]
  } ]
}, {
  "timing" : [ {
    "times" : [ {
      "from" : "07:30:02",
      "to" : "17:30:02"
    } ]
  }, {
    "times" : [ {
      "from" : "07:30:04",
      "to" : "17:30:04"
    } ]
  } ],
  "id" : "2",
  "name" : "data2"
} ]

If you see the JSON array first object with id 1 contains times end with "01" & "03" and second object with id 2 contains times end with "02" & "04". I am expecting "01" & "02" and "03" & "04" respectively.


Solution

  • I think you were mixing up the parent array indexes &n when setting up the new structure , for example:

    setting name, id is correct because you need to go up 1 level to get to the parent array , therefore using [&1] is correct.

    setting the opening timing array however is wrong, in [&1].timing the index should be at level 3 now because you need it as part of the top parent array, so you count from the current From,To level at 0 up to 3 to get to the parent.

    Setting the times array "[&3].times[0]" also wrong because the grouping should be similar to the opening array which is at level 1 above. Also Im not sure what is 0 suppose to do in times[0] , instead you should use # to indicate that you are grouping From,To under the same object for each timing.

    Based on the info above the spec should be like this:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].&",
            "opening": {
              "*": {
                "From": "[&3].timing.[&1].times[#].from",
                "To": "[&3].timing.[&1].times[#].to"
              }
            }
          }
        }
      }
    ]