# Convert java query output in json format

**URL:** <https://discuss.elastic.co/t/convert-java-query-output-in-json-format/316893>\
**Category:** Elasticsearch\
**Created:** [October 18, 2022, 1:06pm UTC](https://discuss.elastic.co/t/convert-java-query-output-in-json-format/316893 "2022-10-18T13:06:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubh/32/111180_2.png) [@Shubh](https://discuss.elastic.co/u/Shubh)\
**Post date:** [October 18, 2022, 1:06pm UTC](https://discuss.elastic.co/t/convert-java-query-output-in-json-format/316893/1 "2022-10-18T13:06:31Z")

</div>

Hi all,  
can someone help get my java query output in json format im just printing it in line and now i want to upgrade my code output tried multiple ways but no luck.  
Below is my code elasticsearch java api code.

```auto
 try {
            var sub3TermAggregation = new Aggregation.Builder()
                    .sum(new SumAggregation.Builder()
                            .field("labels.row_count").build())
                    .build();

            var sub4Aggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder()
                            .field("labels.job_id")
                            .size(100000)
                            // .order(List.of("_key", SortOrder.Desc))
                            .minDocCount(1)
                            .build())
                    .aggregations("3", sub3TermAggregation)
                    .build();

            var aggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder()
                            .field("labels.client_id")
                            .size(100000)
                            // .order(List.of("_key", SortOrder.Desc))
                            .minDocCount(1)
                            .build())
                    .aggregations("4", sub4Aggregation)
                    .build();

            var boolquery = BoolQuery
                    .of(b -> b.filter(Query
                            .of(q -> q.range(RangeQuery
                                    .of(r -> r.field("@timestamp")
                                            .gte(JsonData.of("1664755200000"))
                                            .lte(JsonData.of("1664787600000"))
                                            .format("epoch_millis")
                                    ))
                            )).filter(f -> f.queryString(QueryStringQuery.of(qs -> qs
                            .query("service.name:*itm*").analyzeWildcard(true))))
                    );
            var query = Query.of(q -> q.bool(boolquery));

            var searchRequest = new SearchRequest.Builder()
                    .index("*itm*")
                    .size(0)
                    .query(query)
                    .aggregations("2", aggregation)
                    .build();

            var response = client.search(searchRequest, Void.class);

            var aggs2 = response.aggregations().get("2");
            if (Objects.nonNull(aggs2)) {
                var buckets = aggs2.sterms().buckets().array();
                for (StringTermsBucket bucket : buckets) {
                    var subAggs4 = bucket.aggregations().get("4");
                    subAggs4.sterms().buckets().array().forEach(agg3Bucket -> {
                        System.out.println(bucket.key() + bucket.docCount() + "," +
                                agg3Bucket.key() + agg3Bucket.docCount() + "," + agg3Bucket.aggregations().get("3").sum().value());
                    });

                }

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

```

And here is the example output of query. it is printing like client\_id,job\_id,sum\_count

```auto
24311,5660901,1428.0
255641,5660931,4260.0
340441,17090711,1051574.0
344691,17091121,13613.0
373201,17091971,2769.0
381221,5661261,277579.0
516391,5661071,1761.0
528191,5660691,428.0

```

And now i want to upgarde my code for future updates for eg. like make a csv of output. and for that output should be json.

Here the json example im trying to archive from my code.

```auto
{
  "result": [
    {
      "client_id": "24311",
      "job_id": "5660901",      
      "sum_count": "1428.0"
    },
    {
      "client_id": "24311",
      "job_id": "5660931",      
      "sum_count": "1428.0"
    },
    {
       "client_id": "24311",
      "job_id": "5660901",      
      "sum_count": "1428.0"
    }
  ]
}

```

Can someone take a look into this it would be very great. if there is anything req. pls let me know.  
Thanks.

---

<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:** [November 15, 2022, 1:06pm UTC](https://discuss.elastic.co/t/convert-java-query-output-in-json-format/316893/2 "2022-11-15T13:06:59Z")

</div>

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