# AggregationBuilders throws an error JsonGenerationException

**URL:** <https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090>\
**Category:** Elasticsearch\
**Created:** [March 27, 2017, 8:27am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090 "2017-03-27T08:27:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravindtga](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@aravindtga](https://discuss.elastic.co/u/aravindtga)\
**Post date:** [March 27, 2017, 8:27am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/1 "2017-03-27T08:27:29Z")

</div>

I am using elasticsearch API (elasticsearch5.2.1.jar)to do the aggregation query as follows,

`StatsAggregationBuilder statAggBuild = AggregationBuilders.stats("agg").field("ipAddress");`

This returns the follows exception

`{ "error" : "JsonGenerationException[Can not write a field name, expecting a value]"}`

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [March 27, 2017, 9:24am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/2 "2017-03-27T09:24:41Z")

</div>

Computing average on an IP address?

---

<div class="post-metadata">

**Author:** ![aravindtga](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@aravindtga](https://discuss.elastic.co/u/aravindtga)\
**Post date:** [March 27, 2017, 9:51am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/3 "2017-03-27T09:51:16Z")

</div>

Getting the Count from IP address.

Also I have tried ValueCountAggregationBuilder, It is also throwing the same error

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [March 27, 2017, 10:14am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/4 "2017-03-27T10:14:40Z")

</div>

I might need to see more of your client code to recreate.

I suggest looking at the `cardinality` agg for more effiicient counting of uniques .

---

<div class="post-metadata">

**Author:** ![aravindtga](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@aravindtga](https://discuss.elastic.co/u/aravindtga)\
**Post date:** [March 28, 2017, 8:03am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/5 "2017-03-28T08:03:38Z")

</div>

```
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder;

public class TestFile 
{
	public static void main(String[] args) 
	{
		try 
		{
			StatsAggregationBuilder statAggBuild = AggregationBuilders.stats("agg").field("clientip");
			
			System.out.println(statAggBuild);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

```

Above is the code i am using, It throws the error as follows,

`{ "error" : "JsonGenerationException[Can not write a field name, expecting a value]"}`

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [March 28, 2017, 8:05am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/6 "2017-03-28T08:05:45Z")

</div>

Try this:

```
    TransportClient client = new PreBuiltXPackTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));               
    StatsAggregationBuilder aggs = AggregationBuilders.stats("stats").field("rating");
    SearchResponse searchResponse = client.prepareSearch("reviews").addAggregation(aggs).get();
    InternalStats stats = searchResponse.getAggregations().get("stats");
    System.out.println("Average stat =" + stats.getAvg());
    client.close();
```

---

<div class="post-metadata">

**Author:** ![aravindtga](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@aravindtga](https://discuss.elastic.co/u/aravindtga)\
**Post date:** [March 28, 2017, 11:58am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/7 "2017-03-28T11:58:12Z")

</div>

It works as expected.

I think there is a problem in the toString method. Which throws the exception while called.

---

<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 25, 2017, 11:58am UTC](https://discuss.elastic.co/t/aggregationbuilders-throws-an-error-jsongenerationexception/80090/8 "2017-04-25T11:58:13Z")

</div>

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