# How can I get the Json DSL from a Query object in java client

**URL:** https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552
**Category:** OpenSearch Client Libraries
**Tags:** opensearch-java
**Created:** [November 20, 2022, 3:31pm UTC](https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552 "2022-11-20T15:31:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![leonardo89](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@leonardo89](https://forum.opensearch.org/u/leonardo89)
#### Post date: [November 20, 2022, 3:31pm UTC](https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552/1 "2022-11-20T15:31:13Z")

</div>

I’m using the last version of OpenSearch (2.4.x) and I want to know if it is possible to get the Json DSL of a query performed. For example:

I have this query in the java client

```auto
Query query = new Query.Builder()
                .match(new MatchQuery.Builder()
                        .field("machine.os.keyword")
                        .query(q -> q.stringValue("osx"))
                        .build())
                .build();

```

I want to get the DSL equivalent

```auto
{
  "query": {
    "match": {
      "machine.os.keyword": "osx"
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)
#### Post date: [November 22, 2022, 6:08am UTC](https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552/2 "2022-11-22T06:08:11Z")

</div>

There is an index-level setting that allows you to enable query logs. It is set to -1 by default, but you can change it to a nonnegative value: [Logs - OpenSearch documentation](https://opensearch.org/docs/latest/opensearch/logs/)

---

<div class="post-metadata">

### Author: ![leonardo89](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@leonardo89](https://forum.opensearch.org/u/leonardo89)
#### Post date: [November 22, 2022, 8:58am UTC](https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552/3 "2022-11-22T08:58:37Z")

</div>

Thanks for your response, @wbeckler, but I am looking for a specific function in the OpenSearch java client to get the JSON version of an elasticsearch query from a Query object as I say in the example above

---

<div class="post-metadata">

### Author: ![mnb4feb](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@mnb4feb](https://forum.opensearch.org/u/mnb4feb)
#### Post date: [August 3, 2023, 7:35pm UTC](https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552/4 "2023-08-03T19:35:16Z")

</div>

Hey @leonardo89 , Have you managed to do that ? Could you please share it ?

---

<div class="post-metadata">

### Author: ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)
#### Post date: [August 4, 2023, 2:34pm UTC](https://forum.opensearch.org/t/how-can-i-get-the-json-dsl-from-a-query-object-in-java-client/11552/5 "2023-08-04T14:34:37Z")

</div>

Take a look here:

> <https://github.com/opensearch-project/opensearch-java/issues/588>
>
> \### Is your feature request related to a problem?
> 
> Add a way to easily seriali…ze queries to JSON for logging.
> 
> \### What solution would you like?
> 
> \`\`\`java
> IndexRequest\<Movie\> indexRequest = new IndexRequest.Builder\<Movie\>()
> .index(index)
> .id("1")
> .document(movie)
> .build();
> 
> logger.info("JSON: {}", indexRequest.toJson());
> \`\`\`
> 
> Working serialization code \[here\](https://github.com/dblock/opensearch-java-client-demo/blob/to-json/src/main/java/Example.java#L34).
> 
> \### Do you have any additional context?
> 
> From a Slack discussion in https://opensearch.slack.com/archives/C051XNH62KB/p1690396113772759
