# JSON Webhooks

**URL:** <https://discuss.elastic.co/t/json-webhooks/1024>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [May 20, 2015, 7:16pm UTC](https://discuss.elastic.co/t/json-webhooks/1024 "2015-05-20T19:16:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![flecno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flecno/32/44811_2.png) [@flecno](https://discuss.elastic.co/u/flecno)\
**Post date:** [May 20, 2015, 7:16pm UTC](https://discuss.elastic.co/t/json-webhooks/1024/1 "2015-05-20T19:16:52Z")

</div>

Hey,

I try to build a simple Hipchat notification by using webhooks. If I use simple static messages my notifications arrive. How do I create a JSON payload body with mustache? I tried the follwing configuration:

```auto
"webhook" : {
    "method" : "POST", 
    "scheme": "https",
    "host" : "api.hipchat.com",
    "port": 443,
    "path": "/v2/room/watchers/notification", 
    "headers" : {
      "Authorization" : "Bearer ROOM_TOKEN",
      "Content-Type": "application/json"
    },
    "body" : "{ \"message\": \"{{ctx.payload.hits.total_hits}}\" }"
}

```

But the notifications does not arrive my room. If I use a static `"body" : "{ \"message\": \"xxx\" }"` I receive my messages.

So how to build JSON payload for my body? It seems the template would now work with my JSON curly braces.

---

<div class="post-metadata">

**Author:** ![flecno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flecno/32/44811_2.png) [@flecno](https://discuss.elastic.co/u/flecno)\
**Post date:** [May 20, 2015, 7:43pm UTC](https://discuss.elastic.co/t/json-webhooks/1024/2 "2015-05-20T19:43:14Z")

</div>

Oh I figured out my problem... The message is empty because there is no field `total_hits`. Your documentation contains many errors. But there is no _Edit_ Button as in your open source products.

If I use `"body" : "{ \"message\": \"{{ctx.payload.hits.total}}\" }"` it's works fine except for the ugly escaped JSON string syntax.

---

<div class="post-metadata">

**Author:** ![GaelTadh](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@GaelTadh](https://discuss.elastic.co/u/GaelTadh)\
**Post date:** [May 20, 2015, 8:54pm UTC](https://discuss.elastic.co/t/json-webhooks/1024/3 "2015-05-20T20:54:26Z")

</div>

Hi Flenco,  
You can also specify the body as a json template like so :

```json
"body" : {
  "inline" : { "message" : "{{ctx.payload.hits.total}}" }
}

```

---

<div class="post-metadata">

**Author:** ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)\
**Post date:** [May 20, 2015, 9:41pm UTC](https://discuss.elastic.co/t/json-webhooks/1024/4 "2015-05-20T21:41:01Z")

</div>

Hi flecno, thanks for reporting this documentation bug. We are fixing it now.

---

<div class="post-metadata">

**Author:** ![mattgolding](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mattgolding](https://discuss.elastic.co/u/mattgolding)\
**Post date:** [June 11, 2015, 12:46am UTC](https://discuss.elastic.co/t/json-webhooks/1024/5 "2015-06-11T00:46:45Z")

</div>

I have the same issue with not being able to deserialize the data in the web server code. I tried @GaelTadh and I did notice a difference with my body code and I can see how that code would work with a single param like the total hits.

My issue however is that I am looking at sending the records that came back in the search as I need to extract some information out of each document and perform some tasks on each one. Is there a way to JSONify the search results to accomplish this? Or is there a better way to do this via a transform or script?

---

<div class="post-metadata">

**Author:** ![ShinJJang](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@ShinJJang](https://discuss.elastic.co/u/ShinJJang)\
**Post date:** [September 11, 2015, 4:37am UTC](https://discuss.elastic.co/t/json-webhooks/1024/6 "2015-09-11T04:37:47Z")

</div>

Thanks, @GaelTadh!!!

@GaelTadh's solution is also work to slack 500 error.  
This is my fault webook with slack:

```
....
"body": "payload = { 'text': 'error test\n 400 errors count: {{ctx.payload.hits.total}} \n' blah blah}"
 ...

```

Change to:

```
# In header, content type is 'application/json'
 ....
"body": { "inline": { "text" : "error test\n 400 errors count: {{ctx.payload.hits.total}} \n blah blah"} }
...

```

Hope this is help to someone.

---

<div class="post-metadata">

**Author:** ![Pransh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pransh/32/12806_2.png) [@Pransh](https://discuss.elastic.co/u/Pransh)\
**Post date:** [September 21, 2016, 8:34am UTC](https://discuss.elastic.co/t/json-webhooks/1024/7 "2016-09-21T08:34:20Z")

</div>

Hi

is there a way to send the complete payload received through webhook as JSON  
i want to send the complete payload i received based on conditions

---

<div class="post-metadata">

**Author:** ![iantayrob](https://avatars.discourse-cdn.com/v4/letter/i/c68b51/32.png) [@iantayrob](https://discuss.elastic.co/u/iantayrob)\
**Post date:** [October 28, 2016, 5:01pm UTC](https://discuss.elastic.co/t/json-webhooks/1024/8 "2016-10-28T17:01:56Z")

</div>

I have the exact same question as Pransh. Can someone please answer it? The webhook data is not returning as JSON and I want to know why and what can be done about it. Otherwise, I will need to create a script to do the conversion, but that should be an unnecessary step in the pipeline.

---

<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:** [October 31, 2016, 6:54am UTC](https://discuss.elastic.co/t/json-webhooks/1024/9 "2016-10-31T06:54:51Z")

</div>

Hey,

please open new subjects instead of reviving old threads - a lot has changed since almost one and a half years 🙂

First, if the data returned is not JSON, and you want to it to be JSON you need to convert it.  
Since Elasticsearch 2.4 there is an additional mustache helper handler called [toJson](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_converting_parameters_to_json), which may help you.

--Alex

---

<div class="post-metadata">

**Author:** ![Pransh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pransh/32/12806_2.png) [@Pransh](https://discuss.elastic.co/u/Pransh)\
**Post date:** [November 10, 2016, 6:58am UTC](https://discuss.elastic.co/t/json-webhooks/1024/10 "2016-11-10T06:58:11Z")

</div>

Hi iantayrob

"condition" : {  
"compare" : {  
"ctx.payload.hits.total" : {  
"gt" : 0  
}  
}  
},  
"transform" : {  
"script" : "return [body: groovy.json.JsonOutput.toJson(ctx.payload)]"  
},

Use this and you will receive the payload in JSON

---

<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:42pm UTC](https://discuss.elastic.co/t/json-webhooks/1024/11 "2017-07-06T13:42:09Z")

</div>


