# Render Json strings from Elastic API client objects

**URL:** <https://discuss.elastic.co/t/render-json-strings-from-elastic-api-client-objects/341238>\
**Category:** Elasticsearch\
**Created:** [August 21, 2023, 12:44pm UTC](https://discuss.elastic.co/t/render-json-strings-from-elastic-api-client-objects/341238 "2023-08-21T12:44:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zer0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zer0/32/124706_2.png) [@Zer0](https://discuss.elastic.co/u/Zer0)\
**Post date:** [August 21, 2023, 12:44pm UTC](https://discuss.elastic.co/t/render-json-strings-from-elastic-api-client-objects/341238/1 "2023-08-21T12:44:57Z")

</div>

Hi,

I am using the Elasticsearch API client (8.9) for java and wondering how to render those Queries and Responses as json strings.  
For example I can do a simple query like so:

```auto
val query = Query.of { q -> q.matchAll { it } }
val request = SearchRequest.of {
   it.query(query)
  it.index("some_index")
}
val response = esClient.search(request, Any::class.java)

val queryJson = query.toString() // this yields the json but there is "Query: " prepended
val responseJson = response.toString() // this yields the json but there is "SearchResponse: " prepended

val mapper = ObjectMapper()
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
val string = mapper.writeValueAsString(result) // this works

```

Since the client can render the whole thing as json in the `toString` method the capability is there. There is just a type annotation prepended and I can't find a method to just render the whole thing as json.  
Jackson ObjectMapper can render it if I configure it to render fields and not look for getters, but this seems to be unecessarily complicated the the objects themselven _can_ render as json.  
So my question is, what is the best way to get all these things as Json strings with an onboard method or do I really have to rely on an external Json mapper?

Cheers and thanks!

---

<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:** [August 21, 2023, 1:33pm UTC](https://discuss.elastic.co/t/render-json-strings-from-elastic-api-client-objects/341238/2 "2023-08-21T13:33:45Z")

</div>

Have a look at this class:

```
co.elastic.clients.json.JsonpUtils

```

There's a toJsonString() method.

---

<div class="post-metadata">

**Author:** ![Zer0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zer0/32/124706_2.png) [@Zer0](https://discuss.elastic.co/u/Zer0)\
**Post date:** [August 21, 2023, 1:59pm UTC](https://discuss.elastic.co/t/render-json-strings-from-elastic-api-client-objects/341238/3 "2023-08-21T13:59:53Z")

</div>

Thanks again for the fast reply, it works like a charm. Yields the same results as objectMapper but I feel way more comfortable using an ES scoped class for this than a generic mapper!

---

<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:** [September 18, 2023, 2:00pm UTC](https://discuss.elastic.co/t/render-json-strings-from-elastic-api-client-objects/341238/4 "2023-09-18T14:00:19Z")

</div>

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