# Reusing internal implementation to transform JSON mapping to ES mapping

**URL:** https://discuss.elastic.co/t/reusing-internal-implementation-to-transform-json-mapping-to-es-mapping/300597
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [March 24, 2022, 3:26pm UTC](https://discuss.elastic.co/t/reusing-internal-implementation-to-transform-json-mapping-to-es-mapping/300597 "2022-03-24T15:26:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vred](https://avatars.discourse-cdn.com/v4/letter/v/e99b99/32.png) [@vred](https://discuss.elastic.co/u/vred)
#### Post date: [March 24, 2022, 3:26pm UTC](https://discuss.elastic.co/t/reusing-internal-implementation-to-transform-json-mapping-to-es-mapping/300597/1 "2022-03-24T15:26:15Z")

</div>

Hello all, I have a use case where I have to do some validation on the JSON mapping passed into the PUT request body. There are several keywords (like dynamic, include\_in\_parent, etc.) and I cannot account for them all when I do my validations and it would be preferable to use the ES defined type `Map<String, Property>`. Is it possible to re-use the internal serialization that ES uses to make this data structure? If not, could someone please point me to the file it occurs in the github repo?

Update: This is in reference to the new Java client.

Thank you kindly,  
Vlad

---

<div class="post-metadata">

### Author: ![vred](https://avatars.discourse-cdn.com/v4/letter/v/e99b99/32.png) [@vred](https://discuss.elastic.co/u/vred)
#### Post date: [March 29, 2022, 8:38pm UTC](https://discuss.elastic.co/t/reusing-internal-implementation-to-transform-json-mapping-to-es-mapping/300597/2 "2022-03-29T20:38:58Z")

</div>

Not sure if my ask was somewhat unclear. Is serializing the `mappings` object provided in the `PUT _mapping` API body using the existing ES serializer possible? I've searched for it in the github repo but I couldn't find the point at which the JSON object `mappings` is turned into an Elasticsearch specific type (namely `Map<String, Property>`).  
I require this because I want to validate that changes to the mapping have a specific format.

My alternative, and the way I've currently implemented this is to collapse the `mappings` object in dot-notation and remove certain Elasticsearch-specific keywords; like `properties.`, `.type`, etc. This solution doesn't scale because there is a chance I didn't account for every keyword.  
However collapsing the `Map<String, Property>` used by Elasticsearch internally (and returned in the java client from `getMappingRequest`) relies on Elasticsearch correctly parsing the `mappings` object.

Thanks

---

<div class="post-metadata">

### Author: ![vred](https://avatars.discourse-cdn.com/v4/letter/v/e99b99/32.png) [@vred](https://discuss.elastic.co/u/vred)
#### Post date: [March 30, 2022, 5:24pm UTC](https://discuss.elastic.co/t/reusing-internal-implementation-to-transform-json-mapping-to-es-mapping/300597/3 "2022-03-30T17:24:04Z")

</div>

If anyone has a similar question in the future; `withJson` is a general deserializer than will convert a JSON to internal ES objects without having to make a request to the cluster itself.

```auto
  public static Map<String, Property> jsonToMapping(String json) {
    TypeMapping typeMapping = withJson(new TypeMapping.Builder(), new StringReader(json), new JacksonJsonpMapper()).build();
    return typeMapping.properties();
  }

  private static <T, B extends ObjectBuilder<T>> B withJson(
      B builder, Reader json, JsonpMapper mapper) {
    // Find which deserializer is needed
    JsonpDeserializer<?> classDeserializer =
        JsonpMapperBase.findDeserializer(builder.getClass().getEnclosingClass());

    @SuppressWarnings("unchecked")
    ObjectDeserializer<B> builderDeserializer =
        (ObjectDeserializer<B>) DelegatingDeserializer.unwrap(classDeserializer);

    JsonParser parser = mapper.jsonProvider().createParser(json);
    builderDeserializer.deserialize(builder, parser, mapper, parser.next());
    return builder;
  }

```

---

<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: [April 27, 2022, 5:24pm UTC](https://discuss.elastic.co/t/reusing-internal-implementation-to-transform-json-mapping-to-es-mapping/300597/4 "2022-04-27T17:24:14Z")

</div>

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