# Problem with script in transform accessing an array

**URL:** https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [June 21, 2015, 8:24am UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043 "2015-06-21T08:24:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)
#### Post date: [June 21, 2015, 8:24am UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/1 "2015-06-21T08:24:24Z")

</div>

Hi, I want to return the length of an array in a transform script within the logging action. I am doing this:

```
  "actions": {
    "log": {
      "transform": {
        "script": "return [total_snapshots : ctx.payload.snapshots.length]"
      },
      "logging": {
        "text": "Found {{ctx.payload.total_snapshots}} snapshots at {{ctx.execution_time}}"
      }
    }
  }

```

I can get back an array of objects using ct..payloaad.snapshots.snapshot but I want to have the length of the array. Does someone know whether this is possible and how?

Thanks.

---

<div class="post-metadata">

### Author: ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)
#### Post date: [June 21, 2015, 8:50pm UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/2 "2015-06-21T20:50:04Z")

</div>

I found the way to make this work, can't stop thinking I must have tried this before. Still it does work now with the following code:

```
  "actions": {
    "log": {
      "transform": {
        "script": "return [total_snapshots : ctx.payload.snapshots.size()]"
      },
      "logging": {
        "text": "Found {{ctx.payload.total_snapshots}} snapshots at {{ctx.execution_time}}"
      }
    }
  }
```

---

<div class="post-metadata">

### Author: ![skearns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skearns/32/125945_2.png) [@skearns](https://discuss.elastic.co/u/skearns)
#### Post date: [June 21, 2015, 9:02pm UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/3 "2015-06-21T21:02:54Z")

</div>

Hi Jettro,

I'm glad you figured it out! Thanks for sharing the working solution 😄

---

<div class="post-metadata">

### Author: ![uboness](https://avatars.discourse-cdn.com/v4/letter/u/96bed5/32.png) [@uboness](https://discuss.elastic.co/u/uboness)
#### Post date: [June 24, 2015, 5:09pm UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/4 "2015-06-24T17:09:18Z")

</div>

Jettro, looks like `snapshots` is not an array, but a list? Is it by any chance coming from a json payload? for real "arrays" `.length` should indeed work (as well as `size()`)

---

<div class="post-metadata">

### Author: ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)
#### Post date: [June 24, 2015, 8:40pm UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/5 "2015-06-24T20:40:00Z")

</div>

The payload is coming from this response, it looks line an array to me in Json. Not sure what groovy does with it though. From the jsonslurper documentation I read they convert arrays to lists. In that case what you're saying @url sounds like the reason. It is a List not and array.

```
{
   "snapshots": [
      {
         "snapshot": "conferences-20150619094018", //removed other fields
      },
      {
         "snapshot": "conferences-20150619113941",
      },
      {
         "snapshot": "conferences-20150621223930",
      },
      {
         "snapshot": "conferences-20150621224116",
      }
   ]
}
```

---

<div class="post-metadata">

### Author: ![uboness](https://avatars.discourse-cdn.com/v4/letter/u/96bed5/32.png) [@uboness](https://discuss.elastic.co/u/uboness)
#### Post date: [June 24, 2015, 11:09pm UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/6 "2015-06-24T23:09:31Z")

</div>

so if it's coming from json, then actually its watcher that converts the json arrays to lists :)... I just wasn't sure where you're gettings `snapshots` from (it could have been from a script transform)

---

<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: [July 6, 2017, 1:49pm UTC](https://discuss.elastic.co/t/problem-with-script-in-transform-accessing-an-array/24043/7 "2017-07-06T13:49:25Z")

</div>


