# Ways to build Json doc in ES8 Java API client

**URL:** <https://discuss.elastic.co/t/ways-to-build-json-doc-in-es8-java-api-client/314459>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 14, 2022, 7:53pm UTC](https://discuss.elastic.co/t/ways-to-build-json-doc-in-es8-java-api-client/314459 "2022-09-14T19:53:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijaxahmed](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ijaxahmed](https://discuss.elastic.co/u/ijaxahmed)\
**Post date:** [September 14, 2022, 7:53pm UTC](https://discuss.elastic.co/t/ways-to-build-json-doc-in-es8-java-api-client/314459/1 "2022-09-14T19:53:33Z")

</div>

How to build Json document In Elasticsearch 8 API client , as In older Client APIs its possible to build using XContentBuilder like

````auto
XContentBuilder builder = jsonBuilder()
    .startObject()
        .field("firstName", "jhon")
        .field("lastname", "doe")
        .field("message", "trying out Elasticsearch")
    .endObject()```

````

But now in ES8 Java API Client i do not see any way to build Json doc, for example if i want to insert a "Student" into "School" index , then possible ways are

1- using direct bean as document

```auto
esClient.index(i -> i
    .index("school")
    .id(product.getId())
    .document(student)

```

2- using pre builded json as string (no way to build explicitly)

```auto
Reader input = new StringReader(
    "{'@timestamp': '2022-04-08T13:55:32Z', 'firstName': 'jhone', 'lastName': 'doe'}"
    .replace('\'', '"'));

IndexRequest<JsonData> request = IndexRequest.of(i -> i
    .index("school")
    .withJson(input)
);

```

3- using JsonData class

```auto
JsonpMapper jsonpMapper = esClient.jsonpMapper();
JsonData jsonData = JsonData.of(student , jsonpMapper);

esClient.index(i -> i
    .index("school")
    .id(product.getId())
    .document(jsonData)

```

so is there any way to build Json using Java API Client and input as doc to index

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [September 15, 2022, 2:22pm UTC](https://discuss.elastic.co/t/ways-to-build-json-doc-in-es8-java-api-client/314459/2 "2022-09-15T14:22:09Z")

</div>

Indeed, there is no way to programmatically build JSON in the Java API client, and this is a deliberate omission.

`JsonData` can hold any object that can be serialized to JSON: this can be a map, a domain object that will be serialized to JSON, etc.

The `withJson` method is meant to load JSON from text-based sources, like copy/pasting from the Kibana dev console. For example:

```java
        var object = Map.ofEntries(
            Map.entry("firstName", "John"),
            Map.entry("lastName", "Doe"),
            Map.entry("message", "trying out Elasticsearch")
        );

        esClient.index(i -> i
            .index("school")
            .id(product.getId())
            .document(JsonData.of(object))
        );

```

Hope this helps.

---

<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:** [October 13, 2022, 2:22pm UTC](https://discuss.elastic.co/t/ways-to-build-json-doc-in-es8-java-api-client/314459/3 "2022-10-13T14:22:44Z")

</div>

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