# Need help with jq aggregation filter

**URL:** <https://discuss.elastic.co/t/need-help-with-jq-aggregation-filter/223240>\
**Category:** Elasticsearch\
**Created:** [March 12, 2020, 3:55am UTC](https://discuss.elastic.co/t/need-help-with-jq-aggregation-filter/223240 "2020-03-12T03:55:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blason](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blason/32/42284_2.png) [@Blason](https://discuss.elastic.co/u/Blason)\
**Post date:** [March 12, 2020, 3:55am UTC](https://discuss.elastic.co/t/need-help-with-jq-aggregation-filter/223240/1 "2020-03-12T03:55:01Z")

</div>

Hi Folks,

I understand this might be off-topic but need help on using jq with my bash script for extracting IP addresses. Somehow my filter is not working hence needed your help. I am extracting src\_ip for last 24 hours.

```auto
function test () {
curl --insecure -u xxxx:xxxx -s -XGET "https://127.0.0.1:16577/logstash-*/_search" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "2": {
      "terms": {
        "field": "src_ip.keyword",
        "order": {
          "_count": "desc"
        },
        "size": 5
      }
    }
  },
  "size": 0,
  "_source": {
    "excludes": []
  },
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "docvalue_fields": [
    {
      "field": "@timestamp",
      "format": "date_time"
    },
    {
      "field": "timestamp",
      "format": "date_time"
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-1d/d",
              "lte": "now/d"
            }
          }
        }
      ],
      "filter": [
        {
          "match_all": {}
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}'
}

test

```

Here is my output

```auto
./test.sh | jq
{
  "took": 27,
  "timed_out": false,
  "_shards": {
    "total": 97,
    "successful": 97,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10000,
      "relation": "gte"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "2": {
      "doc_count_error_upper_bound": 1448,
      "sum_other_doc_count": 78005,
      "buckets": [
        {
          "key": "202.179.3.118",
          "doc_count": 19115
        },
        {
          "key": "103.212.19.210",
          "doc_count": 16650
        },
        {
          "key": "202.12.82.23",
          "doc_count": 7305
        },
        {
          "key": "103.207.4.42",
          "doc_count": 7119
        },
        {
          "key": "202.164.48.234",
          "doc_count": 6001
        }
      ]
    }
  }
}

```

But I need only IP addresses and here I am sending jq aggs but unable to deal with.

```auto
./test.sh | jq '.aggregations.2.buckets[].key'
jq: error: Invalid numeric literal at EOF at line 1, column 3 (while parsing '.2.') at <top-level>, line 1:
.aggregations.2.buckets[].key
jq: error: syntax error, unexpected LITERAL, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.aggregations.2.buckets[].key
jq: 2 compile errors
(23) Failed writing body

./test.sh | jq '.aggregations.buckets[].key'
jq: error (at <stdin>:0): Cannot iterate over null (null)

```

---

<div class="post-metadata">

**Author:** ![Blason](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blason/32/42284_2.png) [@Blason](https://discuss.elastic.co/u/Blason)\
**Post date:** [March 12, 2020, 3:58am UTC](https://discuss.elastic.co/t/need-help-with-jq-aggregation-filter/223240/2 "2020-03-12T03:58:34Z")

</div>

Ok - Here is what I did and seems to have resolved the issue. But still wondering why that "2" did not work?

```auto
{
  "aggs": {
    "ips": { <===== Replace 2 with ips
      "terms": {
        "field": "src_ip.keyword",
        "order": {
          "_count": "desc"
        },
        "size": 5
      }
    }
  },

```

---

<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:** [March 12, 2020, 10:32am UTC](https://discuss.elastic.co/t/need-help-with-jq-aggregation-filter/223240/3 "2020-03-12T10:32:10Z")

</div>

Some JSON/scripting tools (like our mustache integration) treats numbers as index numbers for an array instead of a regular name. You might have hit something similar here as well.

---

<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:** [April 9, 2020, 10:32am UTC](https://discuss.elastic.co/t/need-help-with-jq-aggregation-filter/223240/4 "2020-04-09T10:32:13Z")

</div>

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