# How to use the ElasticSearch Java API to build an Aggregation from JSON

**URL:** https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773
**Category:** Elasticsearch
**Created:** [January 6, 2017, 10:40am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773 "2017-01-06T10:40:21Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 7, 2018, 3:01am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/6 "2018-04-07T03:01:38Z")

</div>

Updating this old thread as @monde shared with me a solution he found on [SOF](https://stackoverflow.com/questions/34889026/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json):

```
 val aggsRequestBuilder = new SearchRequestBuilder(client)
  .setIndices(index())
  .setQuery(QueryBuilders.wrapperQuery(query.toString())
  .setAggregations(agg.toString().getBytes())
```

---

_[View the full topic](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773)._
