# How get object in script in aggregations

**URL:** <https://discuss.elastic.co/t/how-get-object-in-script-in-aggregations/268082>\
**Category:** Elasticsearch\
**Created:** [March 23, 2021, 11:13am UTC](https://discuss.elastic.co/t/how-get-object-in-script-in-aggregations/268082 "2021-03-23T11:13:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marina\_S](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Marina\_S](https://discuss.elastic.co/u/Marina_S)\
**Post date:** [March 23, 2021, 11:13am UTC](https://discuss.elastic.co/t/how-get-object-in-script-in-aggregations/268082/1 "2021-03-23T11:13:33Z")

</div>

I have this object which have ArrayList wirh objects

```auto
"sizeData" : {
           "id" : 1,
           "elasticSizeScale" : {
             "id" : 3,
             "sizes" : [
               {
                 "id" : 321,
                 "sizeOrder" : 2
               },
               {
                 "id" : 323,
                 "sizeOrder" : 4
               },
               {
                 "id" : 322,
                 "sizeOrder" : 3
               }
             ]
           }
         }

```

How do i get the object in List? How to use foreach to retrieve an object from a list?  
I need " "id" : 321, "sizeOrder" : 2""

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 23, 2021, 11:43am UTC](https://discuss.elastic.co/t/how-get-object-in-script-in-aggregations/268082/2 "2021-03-23T11:43:28Z")

</div>

If you want to get object 1, I guess you can do something like:

```auto
sizes[0]

```

Not sure if this is what you are looking for though. What is the use case here?

---

<div class="post-metadata">

**Author:** ![Marina\_S](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Marina\_S](https://discuss.elastic.co/u/Marina_S)\
**Post date:** [March 23, 2021, 12:26pm UTC](https://discuss.elastic.co/t/how-get-object-in-script-in-aggregations/268082/3 "2021-03-23T12:26:31Z")

</div>

Not really. I use 'for' to go through all objects from the list

```auto
for(int i=0; i<doc['sizeData.elasticSizeScale.sizes.id'].length; i++){
  doc['sizeData.elasticSizeScale.sizes.id'][i];
}

```

but i can get only one field, not all object. I need get all object

---

<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:** [April 20, 2021, 12:27pm UTC](https://discuss.elastic.co/t/how-get-object-in-script-in-aggregations/268082/4 "2021-04-20T12:27:28Z")

</div>

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