# Syntax error in painless script in watcher

**URL:** <https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [September 16, 2021, 11:42pm UTC](https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438 "2021-09-16T23:42:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcaillat](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bcaillat](https://discuss.elastic.co/u/bcaillat)\
**Post date:** [September 16, 2021, 11:42pm UTC](https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438/1 "2021-09-16T23:42:12Z")

</div>

Hello!  
I'm trying to create a watcher with a condition that uses a Map and keep getting a syntax error.  
For example the following code does not compile:

```auto
  "condition": {
    "script": {
      "source": """
        Map test = [:];
        for (entry in test.entrySet())
        {
            
        }
        return false;
        """,
      "lang": "painless"
    }

```

I tried various combination of "for (t in test)" or "for (def t : test)", but the only thing that works is:

```auto
        Map test = [:];
        for (def i=0; i<test.size(); i++)
        {
            
        }

```

But I'm confused how I am supposed to access the keys from there.  
What is the proper way to access a key/value object like Map in this context?

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [September 22, 2021, 3:11pm UTC](https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438/2 "2021-09-22T15:11:04Z")

</div>

Hi @bcaillat,  
It would be helpful if you posted the actual syntax errors.

Painless does not allow empty foreach loops, which is the problem with the condition script you've posted.

For looping over a map, you can either iterate over the key set or the entry set.

```auto
POST _scripts/painless/_execute
{
  "script": {
    "source": """
        Map test = ["A":"B", "C":"D"];
        String out = "";
        for (entry in test.entrySet())
        {
          out += "[" + entry.getKey() + "::" + entry.getValue() + "] ";
        }
        for (key in test.keySet()) {
          out += "(" + key + "," + test[key] + ") "
        }
        return out;
    """
  }
}

```

Result: `"[A::B] [C::D] (A,B) (C,D) "`

---

<div class="post-metadata">

**Author:** ![bcaillat](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bcaillat](https://discuss.elastic.co/u/bcaillat)\
**Post date:** [October 1, 2021, 6:28pm UTC](https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438/3 "2021-10-01T18:28:30Z")

</div>

Hello @stu ,

thank you so much for your answer. The issue was that my for loop was empty (as I was very ironically just testing the syntax)! As soon as I added a line between the {}, it worked.  
That sounds like a bug in painless interpreter; I would assume that a language should allow empty loop.  
Anyway, thanks a lot for your reply, I was stuck on that for a while!

Ben

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [October 1, 2021, 6:53pm UTC](https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438/4 "2021-10-01T18:53:33Z")

</div>

> [@bcaillat](#):
>
> That sounds like a bug in painless interpreter; I would assume that a language should allow empty loop.

Thanks for circling back. It's an intentional feature to enhance usability and performance. In many painless contexts, the script will run once per document and there may be millions of documents.

---

<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 29, 2021, 6:53pm UTC](https://discuss.elastic.co/t/syntax-error-in-painless-script-in-watcher/284438/5 "2021-10-29T18:53:54Z")

</div>

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