# Loop through ctx.payload.hits.hits in transform

**URL:** <https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [January 15, 2018, 11:14am UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527 "2018-01-15T11:14:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)\
**Post date:** [January 15, 2018, 11:14am UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527/1 "2018-01-15T11:14:27Z")

</div>

Excuse the basic questions but I am new to the product and forum.  
I am attempting to build a watcher that parses a some of the hits messages and updates an index, but I feel my syntax is wrong. Unfortunately I only get told "Watcher: An internal server error occurred".

Lets say my query returns ctx.payload.hits.hits

```
{
  "type":"search",
  "status":"success",
  "payload":{
    "_shards":{
      "total":10,
      "failed":0,
      "successful":10,
      "skipped":0
    },
    "hits":{
      "hits":[
        {
          "_index":"fluentd-2018.01.15",
          "_type":"fluentd",
          "_source":{
            "log":"authlog",
            "@timestamp":"2018-01-15T11:00:01+00:00",
            "message":"blahblah"
          },
          "_id":"XHd6-WABt5EbJmIskQQn"
        }
      ]
    }
  }
}

```

Using a script transform I can create my own key with data from hits  
"transform": {  
"script": {  
"source": "ctx.payload.transform = []; def document = ['bob': ctx.payload.hits.hits[0].\_source.message]; ctx.payload.transform.add(document) ; return ctx.payload.transform;",  
"lang": "painless"  
}  
}

But If I try to introduce a loop I get the server error.  
"transform": {  
"script": {  
"source": "ctx.payload.transform = []; def document = []; for (int j=0;j\<ctx.payload.hits.hits;j++){ document = ['bob': ctx.payload.hits.hits[j].\_source.message]; ctx.payload.transform.add(document) } ; return ctx.payload.transform;",  
"lang": "painless"  
}  
}

What is the correct syntax for iterating around my hits payload and retrieving data?

---

<div class="post-metadata">

**Author:** ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)\
**Post date:** [January 15, 2018, 12:36pm UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527/2 "2018-01-15T12:36:20Z")

</div>

I have discovered my issue. The syntax does not allow for ";" to appear after closing the loop.  
ie  
" ctx.payload.transform.add(document) } ; "  
becomes:  
" ctx.payload.transform.add(document) } "

---

<div class="post-metadata">

**Author:** ![saranyav](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@saranyav](https://discuss.elastic.co/u/saranyav)\
**Post date:** [February 7, 2018, 3:37am UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527/3 "2018-02-07T03:37:41Z")

</div>

Hello Steve,

I have a problem looping through transform script.I am trying to return a field as NA in watcher alert email if that field is not available in \_source document. I have a pre-formatted email template for which the fields that are not available with \_source, I have to show that as NA. I am getting below error.

`Watcher: An internal server error occurred`

1. Is transform script is the right choice to do this? Once I transform this how can I access the returned value in the email alert?

Thanks!  
Saranya

---

<div class="post-metadata">

**Author:** ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)\
**Post date:** [February 7, 2018, 9:14am UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527/4 "2018-02-07T09:14:20Z")

</div>

You'll want to change your double-quotes to single surrounding your scripts string values

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 7, 2018, 9:14am UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527/5 "2018-02-07T09:14:54Z")

</div>

please dont crosspost your issue at other discussions, that are only very partially similar, as you already opened your own discuss issue. Thank you.

---

<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:** [March 7, 2018, 9:14am UTC](https://discuss.elastic.co/t/loop-through-ctx-payload-hits-hits-in-transform/115527/6 "2018-03-07T09:14:56Z")

</div>

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