Access nested field array with scripted field

Hello

My developer created this field structure :

"realization": {
      "id": 24188,
      "releasedOn": null,
      "isAnticipated": false,
      "reason": null,
      "realizationPeriods": [
        {
          "id": 373967,
          "label": "Janvier 2024",
          "realizationPeriodVolumes": [
            {
              "id": 758155,
              "workUnit": {
                "id": 269,
                "label": "my Label",
                "volumeMax": "650.00",
                "volumeAverage": "350.00"
              }
            },
            {
              "id": 758156,
              "workUnit": {
                "id": 270,
                "label": "Heures en entreprise",
                "volumeMax": "140.00",
                "volumeAverage": "70.00"
              }
            }
          ]
        }
	]
}

I need to access this specific field :
realization.realizationPeriods.realizationPeriodVolumes.workUnit.label

I tried with scripted field :

if (doc['realization.realizationPeriods.realizationPeriodVolumes.workUnit.label'].size() == 0) return '';
try {
  return doc['realization.realizationPeriods.realizationPeriodVolumes.workUnit.label.keyword'].value;
} catch(Exception e) {
  return 'N/A';
}

But it returns no values. I tried everything, can't figure what is wrong.

Is this because my "workUnit" field is inside an array ?

Can someone give me the solution ? I spent hours

Thanks !

In my opinion yes.
If you want to retrieve the values from an array, you can do it like this:

 for (int i = 0; i < doc['my_array_field'].length; ++i) {
   // Do something with doc['my_array_field'][i].value
  }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.