# How to parse Watcher/Aggs in JSON format into a table automatically?

**URL:** <https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [September 5, 2020, 9:20am UTC](https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623 "2020-09-05T09:20:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [September 5, 2020, 9:20am UTC](https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623/1 "2020-09-05T09:20:50Z")

</div>

The output of an aggregation in search (or in watcher), currently comes as JSON and contains key-value. The outcome is different and may contain different fields, so hardcoding them into an html body is not possible. Is there a way I can make the output of WATCHER into an html table (action of email) automatically?

For example

```auto
{
    "size": 0,
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-3h"
                    }
                  }
                },
                {
                  "match": {
                    "event.dataset": "sample_web_logs"
                  }
                }
              ]
            }
          },
          "aggs": {
            "group_by_src": {
              "terms": {
                "field": "geo.src",
                "size": 2
              },
              "aggs": {
                "group_by_dest": {
                  "terms": {
                    "field": "geo.dest",
                    "size": 2
                  }
                }
              }
            }
          }
        }

```

The data output is

```auto
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7824,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "group_by_src" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 5086,
      "buckets" : [
        {
          "key" : "CN",
          "doc_count" : 1379,
          "group_by_dest" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 921,
            "buckets" : [
              {
                "key" : "CN",
                "doc_count" : 246
              },
              {
                "key" : "IN",
                "doc_count" : 212
              }
            ]
          }
        },
        {
          "key" : "IN",
          "doc_count" : 1359,
          "group_by_dest" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 827,
            "buckets" : [
              {
                "key" : "IN",
                "doc_count" : 307
              },
              {
                "key" : "CN",
                "doc_count" : 225
              }
            ]
          }
        }
      ]
    }
  }
}

```

I needed the output in the `email` to be an html table without individually picking up the key-value into a hard-coded html

---

<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:** [September 16, 2020, 10:16am UTC](https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623/2 "2020-09-16T10:16:58Z")

</div>

the template language used to write your own emails is mustache. Mustache allows you to loop through a list like the `buckets` array, and then write out the key in the body.

You may want to take a look at the example watches in the examples repo at [https://github.com/elastic/examples/tree/master/Alerting/Sample](https://github.com/elastic/examples/tree/master/Alerting/Sample) Watches

---

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [September 16, 2020, 9:07pm UTC](https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623/3 "2020-09-16T21:07:42Z")

</div>

hi, sorry I may not have been clear. I can work with mustache/HTML, but I have to hard-code lot of these manually. I was mostly asking if the key value pairs can be automatically put into the table (i.e without knowing the key-value field names). So I'm looking for something like as output of aggregation/transform

```auto
<mylogic>
| fields [{key1:value1},{key2:value2},{key3:value3}..]

```

automatically, so in mustache, I can iterate through key/value without hard-coding them

---

<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:** [September 17, 2020, 12:32pm UTC](https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623/4 "2020-09-17T12:32:12Z")

</div>

You would need to dump the whole map data structure at once (but then it's not formatted), or use a transform to convert the random key/value values to something like `{key: 'your_key_alue', value: 'your_value'}` and could then access it stable via `element.key` and `element.value`

hope that helps.

---

<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 15, 2020, 12:32pm UTC](https://discuss.elastic.co/t/how-to-parse-watcher-aggs-in-json-format-into-a-table-automatically/247623/5 "2020-10-15T12:32:17Z")

</div>

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