Accessing subarrays with each operation

Hi everyone,
What would be the best approach to access a sub array using the each operation in a job to push to DHIS2. Below is an example of the input used with all patient identifiable information removed.

{
    "mergedData": [
        {
            "addressNum": "true",
            "aefiEvents": [
                {
                    "aefiDate": "2021-03-04",
                    "chills": "true",
                    "fatigue": "false",
                    "flu": "false",
                    "headache": "true",
                    "jointPain": "false",
                    "musclePain": "true",
                    "other": "false",
                    "otherlist": "",
                    "patientid": 6,
                    "tenderness": "false",
                    "treatment": null,
                    "vomitting": "false"
                }
            ],
            "vaccineEvents": [
                {
                    "batchnum": "4120z023",
                    "comment": null,
                    "coviddose": 1,
                    "covidgiven": "true",
                    "dateoccured": "2021-03-04",
                    "expirydate": "2021-05-29",
                    "nextdate": "2021-04-29",
                    "patientid": 6,
                    "route": "FG1QINt7D0z"
                },
                {
                    "batchnum": "4120z023",
                    "comment": null,
                    "coviddose": 2,
                    "covidgiven": "true",
                    "dateoccured": "2021-04-29",
                    "expirydate": "2021-05-29",
                    "nextdate": null,
                    "patientid": 6,
                    "route": "FG1QINt7D0z"
                }
            ],
            "village": "GIRAUDEL"
        }
    ]
}

The job to create patients and events run successfully. However we are unable to reference the data in “vaccinesEvents” and “aefiEvents” similar to the following:
{ attribute: “jCdBujEEITq”, value: dataValue(“patientId”)},

Line 46 has an attempt at accessing sub array vaccineEvents.

Thank you!

Hello @DanglebenC, The best way to map the dhis2 data model is to map the dhis2 data model first in fn operation, Then use the newly mapped data model with the each() function.

Here is the example

fn((state) => {
  state.dhis2Mapping = state.mergedData.map((data) => {
    return {
      orgunit: "diNawdDsTah",
      trackedEntityType: "MCPQUTHXIZe",
      attributes: [],
      enrollments: [
        {
          orgunit: "c7QFXKlfx DZ",
          program: "SSLp0M0rlU7",
          program5tate: "oRySG82BKE6", // active
          enrollmentDate: data.path_to_date,
          incidentDate: data.path_to_date,
          events: [
            {
              Program: "SSLp(XOrlU7",
              programStage: "bGNvNyvxAv5",
              orgunit: "c7QFXKlfzDZ",
              status: "COMPLETED",
              cwpletedAt: "2019-08-O1T00:00:00.000",
              trackedEntity: "MCPQUTHXIZe",
              datavalues: [
                {
                  dataElement: "DwNnsBq8DgN",
                  value: data.vaccineEvents[0].coviddose,
                },
              ],
            },
          ],
        },
      ],
    };
  });
  return state;
});

each(
  "$.dhis2Mapping[*]",
  create("trackedEntityInstances", (state) => state.data)
);