# How to update nested objects In Elasticsearch 2.2 script via Java API

**URL:** <https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135>\
**Category:** Elasticsearch\
**Created:** [March 1, 2016, 4:54pm UTC](https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135 "2016-03-01T16:54:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeteyPabPro](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PeteyPabPro](https://discuss.elastic.co/u/PeteyPabPro)\
**Post date:** [March 1, 2016, 4:54pm UTC](https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135/1 "2016-03-01T16:54:21Z")

</div>

I'm trying to update a nested object in Elasticsearch via the Java API, using a technique similar to what is outlined [here](http://stackoverflow.com/questions/18360225/elastic-search-is-it-possible-to-update-nested-objects-without-updating-the-ent). The problem is how to pass the json into the script. If I just blindly concatenate the json to the script string as suggested [here](http://stackoverflow.com/questions/23363801/update-nested-field-in-an-index-of-elasticsearch-with-java-api), the Groovy does not compile. If you pass it in directly as a parameter, it just gets parsed as a string. If I try to use JsonSlurper, like:

```auto
String script = "ctx._source.pete = new groovy.json.JsonSlurper().parseText(json)";
Map<String, Object> params = ImmutableMap.of("json", json);
return new Script(script, ScriptService.ScriptType.INLINE, null, params);

```

I get a compilation exception: unable to resolve class groovy.json.JsonSlurper

A further problem with the JsonSlurper approach seems to be that the Elasticsearch team has basically [disabled](https://github.com/elastic/elasticsearch/pull/16808) it in 2.2.

Does anyone know how to pass the json in correctly via the Java API?

---

<div class="post-metadata">

**Author:** ![PeteyPabPro](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PeteyPabPro](https://discuss.elastic.co/u/PeteyPabPro)\
**Post date:** [March 1, 2016, 5:30pm UTC](https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135/2 "2016-03-01T17:30:58Z")

</div>

Thanks to the guys at Elasticsearch for helping me with this. The answer is to convert the JSON to a `Map` and then just pass the `Map` as a param:

```
String script = "ctx._source.pete = jsonMap";
Map<String, Object> jsonMap = new ObjectMapper().readValue(json, HashMap.class);
Map<String, Object> params = ImmutableMap.of("jsonMap", jsonMap);
return new Script(script, ScriptService.ScriptType.INLINE, null, params);

```

I'm using `org.codehaus.jackson.map.ObjectMapper` to do the conversion from JSON to the `Map`.

---

<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:** [July 5, 2017, 11:12pm UTC](https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135/3 "2017-07-05T23:12:23Z")

</div>


