# Get Field Mapping request failing with UnexpectedJsonEventException using the Java API Client

**URL:** <https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 2, 2022, 1:24pm UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746 "2022-05-02T13:24:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbrunot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sbrunot/32/103222_2.png) [@sbrunot](https://discuss.elastic.co/u/sbrunot)\
**Post date:** [May 2, 2022, 1:24pm UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746/1 "2022-05-02T13:24:52Z")

</div>

Hello,

during the process of migrating our code base from the Java High Level REST Client to the Java API Client, we are hitting an issue when trying to retrieve fields mappings.

When running the following code (with an Elasticsearch 7.17.3 server running, and using version 7.16.3 of the Java API Client):

```auto
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ObjectMapper om = new ObjectMapper();
JsonpMapper mapper = new JacksonJsonpMapper(om);
ElasticsearchTransport transport = new RestClientTransport(restClient, mapper);
ElasticsearchClient client = new ElasticsearchClient(transport);
GetFieldMappingRequest gfmRequest = new GetFieldMappingRequest.Builder()
		.index("*")
		.fields("*")
		.build();
GetFieldMappingResponse gfmResponse = client.indices().getFieldMapping(gfmRequest);

```

the following exception is thrown:

```auto
Exception in thread "main" co.elastic.clients.json.UnexpectedJsonEventException: Unexpected JSON event 'KEY_NAME' instead of 'END_OBJECT'
	at co.elastic.clients.json.JsonpUtils.expectEvent(JsonpUtils.java:57)
	at co.elastic.clients.json.JsonpUtils.expectNextEvent(JsonpUtils.java:51)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:197)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:137)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:79)
	at co.elastic.clients.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:43)
	at co.elastic.clients.json.ObjectDeserializer$FieldObjectDeserializer.deserialize(ObjectDeserializer.java:72)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:176)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:137)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:79)
	at co.elastic.clients.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:43)
	at co.elastic.clients.transport.endpoints.DictionaryResponse.lambda$setupDictionaryResponseDeserializer$0(DictionaryResponse.java:148)
	at co.elastic.clients.json.ObjectDeserializer.parseUnknownField(ObjectDeserializer.java:205)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:174)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:137)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:79)
	at co.elastic.clients.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:43)
	at co.elastic.clients.transport.rest_client.RestClientTransport.decodeResponse(RestClientTransport.java:325)
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:291)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:144)
	at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.getFieldMapping(ElasticsearchIndicesClient.java:1062)

```

When calling the REST API directly (GET [http://localhost:9200/](http://localhost:9200/)_/\_mapping/field/_), it returns valid JSON with an HTTP 200 status.

Is it an issue with the Java API Client or an issue with our code ? How can we fix this ?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![sbrunot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sbrunot/32/103222_2.png) [@sbrunot](https://discuss.elastic.co/u/sbrunot)\
**Post date:** [May 2, 2022, 2:06pm UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746/2 "2022-05-02T14:06:30Z")

</div>

An update on this: when providing an actual field name to the Get Mapping Resquest rather than a wildcard in order to get all the fields, there is no exception.

When providing two fields names, there is the exception... So it looks like the client API is expecting a single field mapping in the response, whatever the request.

So the question might be: how to proceed with the Java API Client to request mapping for all fields ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 2, 2022, 3:51pm UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746/3 "2022-05-02T15:51:26Z")

</div>

Hey,

I suppose you are hitting [getFieldMapping error when several fields · Issue #166 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/issues/166) - feel free to add a comment there or subscribe to the issue to get notified when it is fixed.

As a workaround until this is fixed, you could go with the Low Level Rest Client and execute that request using that one.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![sbrunot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sbrunot/32/103222_2.png) [@sbrunot](https://discuss.elastic.co/u/sbrunot)\
**Post date:** [May 3, 2022, 7:14am UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746/4 "2022-05-03T07:14:45Z")

</div>

Thank you Alexander, that is definitely helping. I will look into using the Low Level Rest Client, as [getFieldMapping error when several fields · Issue #166 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/issues/166) doesn't look as if it's going to be solved anytime soon.

---

<div class="post-metadata">

**Author:** ![sbrunot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sbrunot/32/103222_2.png) [@sbrunot](https://discuss.elastic.co/u/sbrunot)\
**Post date:** [May 3, 2022, 7:30am UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746/5 "2022-05-03T07:30:23Z")

</div>

I guess that the issue comes from [elasticsearch-specification/types.ts at 4cbdef1d11586b7d6b27d3744dd15a6f0ce96afb · elastic/elasticsearch-specification · GitHub](https://github.com/elastic/elasticsearch-specification/blob/4cbdef1d11586b7d6b27d3744dd15a6f0ce96afb/specification/indices/get_field_mapping/types.ts) and the definition of a "single key dictionary" for the mappings ?

---

<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:** [May 31, 2022, 7:30am UTC](https://discuss.elastic.co/t/get-field-mapping-request-failing-with-unexpectedjsoneventexception-using-the-java-api-client/303746/6 "2022-05-31T07:30:55Z")

</div>

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