# Convert watches elasticsearch output to json with groovy Ask Question

**URL:** <https://discuss.elastic.co/t/convert-watches-elasticsearch-output-to-json-with-groovy-ask-question/82561>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [April 17, 2017, 9:56am UTC](https://discuss.elastic.co/t/convert-watches-elasticsearch-output-to-json-with-groovy-ask-question/82561 "2017-04-17T09:56:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hedi\_Chahed](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@Hedi\_Chahed](https://discuss.elastic.co/u/Hedi_Chahed)\
**Post date:** [April 17, 2017, 9:56am UTC](https://discuss.elastic.co/t/convert-watches-elasticsearch-output-to-json-with-groovy-ask-question/82561/1 "2017-04-17T09:56:47Z")

</div>

I want to convert payload.hits.hits to json in elasticsearch watcher output. I found as a solution groovy transform script 🙂

```
PUT _watcher/watch/error_alert
{
"trigger": {
  "schedule": {
  "interval": "1m"
}
},
"input": {
"search": {
  "request": {
    "body": {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "default_field": "message",
                "query": "ERROR"
              }
            },
            {
              "range": {
                "@timestamp": {
                  "gte": "now-1m",
                  "lte": "now"
                }
              }
            }
          ]
        }
      }
    }
  }
}
},
"condition": {
  "compare": {
    "ctx.payload.hits.total": {
      "gt": 0
  }
 }
 },
"transform" : {
    "script" : "return [body: groovy.json.JsonOutput.toJson(ctx.payload.hits.hits)]"
},
"actions": {
"some_webhook": {
  "webhook": {
    "method": "POST",
    "host": "*.*.*.*",
    "port": 4000,
    "path": "/sms",
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "message: {{ctx.payload.body}}"
  }
}
}
}

```

This request returns this exception:

```
 "type": "general_script_exception",
"reason": "failed to compile script [ScriptException[compile error]; nested: IllegalArgumentException[Variable [body] is not defined.];]"

```

I use:  
Elasticsearch 5.3.0  
Kibana 5.0.2

---

<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:** [April 21, 2017, 5:35pm UTC](https://discuss.elastic.co/t/convert-watches-elasticsearch-output-to-json-with-groovy-ask-question/82561/2 "2017-04-21T17:35:26Z")

</div>

Hey,

you are using invalid JSON with the body field. Also, you cannot use groovy to create JSON. The solution here is to use the mustache [{{toJson}} directive](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-template.html#_converting_parameters_to_json)

Hope this helps!

--Alex

---

<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:** [May 19, 2017, 5:48pm UTC](https://discuss.elastic.co/t/convert-watches-elasticsearch-output-to-json-with-groovy-ask-question/82561/3 "2017-05-19T17:48:42Z")

</div>

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