# Aggregating array objects using scripted field

**URL:** <https://discuss.elastic.co/t/aggregating-array-objects-using-scripted-field/200514>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [September 21, 2019, 6:44am UTC](https://discuss.elastic.co/t/aggregating-array-objects-using-scripted-field/200514 "2019-09-21T06:44:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thayan\_Oliveira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thayan_oliveira/32/45111_2.png) [@Thayan\_Oliveira](https://discuss.elastic.co/u/Thayan_Oliveira)\
**Post date:** [September 21, 2019, 6:44am UTC](https://discuss.elastic.co/t/aggregating-array-objects-using-scripted-field/200514/1 "2019-09-21T06:44:38Z")

</div>

Hello everyone!

I'm trying to do a transformation in an array object and I'm get stucked. Can anyone help me to figure it out ?

I'm aware that the better solution is doing the "denormalization" of this array as @Joe_Fleming said in another topic, but, unfortunately, it can't be done right now. ([read about it here](https://discuss.elastic.co/t/visualizing-a-json-array-as-datatable/129384)

the evil object

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/3/9361f50dcd83cc15f82628bcb76022e0ea1639df.png)

painless script that isn't working and are giving me a headache.  
def l = new ArrayList();  
if(!doc.containsKey('details.plan\_op\_nature')){return 0;}else{  
for(int i = 0;i\<doc['details.plan\_orders'].values.length;i++){  
l.add(doc['details.plan\_orders\_product\_quantity'].values[i]);  
}  
return sum(l);  
}

The desired result when doc contains the field plan\_op\_nature:  
the sum of all objects in the array l (in the case of the image, it should be 15000)

Thx a lot for your time!

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [September 25, 2019, 8:38am UTC](https://discuss.elastic.co/t/aggregating-array-objects-using-scripted-field/200514/2 "2019-09-25T08:38:02Z")

</div>

Hi @Thayan_Oliveira,

If you create your scripted fields you can do a test run by clicking the "  
Get help with the syntax and preview the results of your script." button and switching to the "Preview Results" tab. What are you seeing there?

A few things I already noticed about your script:

- You can directly sum up the values, no need to collect them in a array list first
- It should be `details.plan_orders.product_quantity` (note the dot instead of the underscore)

Could you try this script:

```auto
if(!doc.containsKey('details.plan_op_nature')){
  return 0;
} else {
  def res = 0;
  for(int i = 0;i<doc['details.plan_orders.product_quantity'].values.length;i++){
    res +=doc['details.plan_orders.product_quantity'].values[i];
  }
  return res;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 23, 2019, 8:38am UTC](https://discuss.elastic.co/t/aggregating-array-objects-using-scripted-field/200514/3 "2019-10-23T08:38:03Z")

</div>

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