Kibana painless scripted fields. Loop on array of objects

The error still the same after l.toArray();

Below is the screenshot of the same. I see only few datatypes

string, number, boolean , date in type dropdown. After checking elastic search logs, ES itself not liking the script and throwing the exception

Caused by: java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(List)int to (Object)boolean