# Map Composite Aggregation Payload in Java using Java Client 8.7

**URL:** <https://discuss.elastic.co/t/map-composite-aggregation-payload-in-java-using-java-client-8-7/330754>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 25, 2023, 2:57pm UTC](https://discuss.elastic.co/t/map-composite-aggregation-payload-in-java-using-java-client-8-7/330754 "2023-04-25T14:57:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [April 25, 2023, 2:57pm UTC](https://discuss.elastic.co/t/map-composite-aggregation-payload-in-java-using-java-client-8-7/330754/1 "2023-04-25T14:57:29Z")

</div>

```auto
{
  "buckets": [
    {
      "key": {
        "SUBJNAME": "ALL",
        "ASOFYEARS": 2018
      },
      "doc_count": 240,
      "cnt_subj": {
        "value": 25521935824
      },
      "cnt_dwnl": {
        "value": 4224
      }
    }
  ]
}

```

I need the output to look like this in Java.

```auto
{
  "SUBJNAME": "ALL",
  "ASOFYEARS": 2018,
  "cnt_subj": 25521935824,
  "cnt_dwnl": 4224
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 25, 2023, 6:58pm UTC](https://discuss.elastic.co/t/map-composite-aggregation-payload-in-java-using-java-client-8-7/330754/2 "2023-04-25T18:58:12Z")

</div>

Hi @tcpeiris

One option is for you to manipulate the response to the desired output. Have you already implemented something?

---

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [April 26, 2023, 7:04am UTC](https://discuss.elastic.co/t/map-composite-aggregation-payload-in-java-using-java-client-8-7/330754/3 "2023-04-26T07:04:41Z")

</div>

```auto
for (CompositeBucket bucket : buckets) {

                    Map<String, Object> subBucket = new LinkedHashMap<>();

                    for (Map.Entry<String, FieldValue> entry : bucket.key().entrySet()) {
                        subBucket.put(entry.getKey(), entry.getValue()._get());
                    }
                    query.getAggregate().getFunctions().forEach(func -> {

                        if (func.getType().equals("sum")) {
                            subBucket.put(func.getName(), bucket.aggregations().get(func.getName()).sum().value());
                        } else if (func.getType().equals("avg")) {
                            subBucket.put(func.getName(), bucket.aggregations().get(func.getName()).avg().value());
                        }

                    });

                }

```

Hi @RabBit_BR, I was able to achieve this by following implementation. Let me know if you have any suggestions.

---

<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:** [May 24, 2023, 7:04am UTC](https://discuss.elastic.co/t/map-composite-aggregation-payload-in-java-using-java-client-8-7/330754/4 "2023-05-24T07:04:55Z")

</div>

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