# How to build a JsonData object (JsonGenerator, etc.) to be used in new elasticsearch java api

**URL:** https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [July 21, 2022, 1:42am UTC](https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224 "2022-07-21T01:42:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)
#### Post date: [July 21, 2022, 1:42am UTC](https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224/1 "2022-07-21T01:42:05Z")

</div>

Anybody here encountered RHLC XContentBuilder?

How is it in migrating to new ElasticSearch java API client?...

Particular implementation is when creating an index.  
In RHLC, we use XContentBuilder to get information from object, and feed this XContentBuilder to a IndexRequest.

How is it done in the new ES java APi client?

Are any sample snippets out there just to guide me in the right direction...

---

<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: [July 21, 2022, 1:17pm UTC](https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224/2 "2022-07-21T13:17:32Z")

</div>

Hi @ALX_DM .

I understand that you use xContentBuilder to create the index. Why with the new client you don't use a file instead of xContentBuilder?

---

<div class="post-metadata">

### Author: ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)
#### Post date: [July 21, 2022, 2:21pm UTC](https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224/3 "2022-07-21T14:21:05Z")

</div>

@RabBit_BR

Yes, I dont use a file in this case.

I have an entity object, and from it, I will create a JsonData.

Im thinking of creating JsonData by:

```auto
      JsonpMapper jsonpMapper = esClient.jsonpMapper();
      JsonProvider jsonProvider = jsonpMapper.jsonProvider();

      StringWriter jsonObjectWriter = new StringWriter();
      try {
          JsonGenerator generator = new JacksonJsonpGenerator(factory.createGenerator(jsonObjectWriter));
          generator.writeStartObject();
          generator.write("name", "ALX DM");
          generator.write("path", "c:/JSON/SAMPLE");
          generator.writeEnd();      
      } catch (IOException e) {
         return null;
      }

      String jsonString = sw.toString(); // convert to json string
      Reader reader = new StringReader(jsonString ); // read the the string

      JsonData jdata = JsonData.from(jsonProvider.createParser(reader), jsonpMapper); //parse

      final var request =
          co.elastic.clients.elasticsearch.core.IndexRequest.of(
              r -> r.index(getIndexName()).id(getAssetId(asset)).document(jdata ));

      esClient.index(request);

```

But as you can see, its kinda inefficient since you have to make a json string, then read it again, then parse it, then put in in `IndexRequest`

Im thinking if there is a way more efficient way of doing this.

---

<div class="post-metadata">

### Author: ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)
#### Post date: [July 21, 2022, 2:29pm UTC](https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224/4 "2022-07-21T14:29:53Z")

</div>

@RabBit_BR

Another way I think is creating JsonData by the object.

Say:

```auto
@Builder
@Data
public class Product {
  private String name;
  private String path;
}

// from other class to create JsonData
:
:

public class IndexDocService {
:
:
   public void index(){

         Product testProduct = Product.builder()
                                                   .name("ALX DM")
                                                   .path("C:/ttest")
                                                   .build();

         JsonpMapper jsonpMapper = esClient.jsonpMapper();
        JsonData jData2 = JsonData.of(testProduct , jsonpMapper);

        final var request =
          co.elastic.clients.elasticsearch.core.IndexRequest.of(
              r -> r.index(getIndexName()).id(getAssetId(asset)).document(jdata ));

      esClient.index(request);
   }

}

```

This I think is ok as it minimizes the process of converting to json, read, parse...

But, maybe there`s another better way...

---

<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: [August 18, 2022, 2:30pm UTC](https://discuss.elastic.co/t/how-to-build-a-jsondata-object-jsongenerator-etc-to-be-used-in-new-elasticsearch-java-api/310224/5 "2022-08-18T14:30:47Z")

</div>

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