# Java API Client - GetResponse source null values converted to String "null"

**URL:** <https://discuss.elastic.co/t/java-api-client-getresponse-source-null-values-converted-to-string-null/314808>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 20, 2022, 7:46pm UTC](https://discuss.elastic.co/t/java-api-client-getresponse-source-null-values-converted-to-string-null/314808 "2022-09-20T19:46:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsorauf](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@nsorauf](https://discuss.elastic.co/u/nsorauf)\
**Post date:** [September 20, 2022, 7:46pm UTC](https://discuss.elastic.co/t/java-api-client-getresponse-source-null-values-converted-to-string-null/314808/1 "2022-09-20T19:46:37Z")

</div>

Hi,

I'm working with the new Java client, v8.3.3. I'm trying to do the following:

- take a source from a `GetResponse<JsonData>`
- create a Jakarta `JsonObjectBuilder` from that `JsonData` source
- use an `ObjectMapper` to convert that `JsonObject` into a Java object (`Foo`)

My problem is that when converting from `JsonObject` to `Foo`, string fields that are null on the source (`JsonValue.ValueType.NULL`) are being set to String `"null"` on the Java object, when they ought to be just set to `null`.

Has anyone else encountered anything similar, or know of a way to handle this?

---

<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 21, 2022, 12:26pm UTC](https://discuss.elastic.co/t/java-api-client-getresponse-source-null-values-converted-to-string-null/314808/2 "2022-09-21T12:26:47Z")

</div>

Can you provide a code snippets so that we have more details to reproduce the issue?

Also, you don't need to go from `JsonData` to `JsonObject` and finally `Foo`: you can directly call `someJsonData.to(Foo.class)`, which will use the object mapper associated to the Elasticsearch client.

---

<div class="post-metadata">

**Author:** ![nsorauf](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@nsorauf](https://discuss.elastic.co/u/nsorauf)\
**Post date:** [September 21, 2022, 1:32pm UTC](https://discuss.elastic.co/t/java-api-client-getresponse-source-null-values-converted-to-string-null/314808/3 "2022-09-21T13:32:25Z")

</div>

I need to add fields (specifically for `_index` and `_id`) to the source, which is why I'm creating a `JsonObjectBuilder` first, before converting to `Foo`.

Here's a snippet of what I'm doing:

```auto
JsonObjectBuilder sourceBuilder = Json.createObjectBuilder(JsonData.of(item.result().source(), _mapper).toJson().asJsonObject());
sourceBuilder.add("_index", item.result().index()).add("_id", item.result().id());
return _modelMapper.map(sourceBuilder.build(), Foo.class)

```

For example, if a String field `name` on Foo is null in the source, the resulting `Foo` object will have name set to `"null"`, and not just `null`.

---

<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 19, 2022, 1:33pm UTC](https://discuss.elastic.co/t/java-api-client-getresponse-source-null-values-converted-to-string-null/314808/4 "2022-10-19T13:33:23Z")

</div>

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