# Error when updating an array element object using java rest client api

**URL:** <https://discuss.elastic.co/t/error-when-updating-an-array-element-object-using-java-rest-client-api/253835>\
**Category:** Elasticsearch\
**Created:** [October 30, 2020, 3:17pm UTC](https://discuss.elastic.co/t/error-when-updating-an-array-element-object-using-java-rest-client-api/253835 "2020-10-30T15:17:05Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![gtoor](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gtoor](https://discuss.elastic.co/u/gtoor)\
**Post date:** [October 30, 2020, 7:44pm UTC](https://discuss.elastic.co/t/error-when-updating-an-array-element-object-using-java-rest-client-api/253835/2 "2020-10-30T19:44:51Z")

</div>

Found a solution based on this post

> [@How to update nested objects In Elasticsearch 2.2 script via Java API](https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135):
>
> 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: String script = "ctx.\_source.pete = new groovy.json.JsonSlurper().parseText(json)"; Map\<String, Object\> …

The solution is to convert your json object to a map using the Jackson ObjectMapper before adding it to the params map.

```auto
    final String updateElementScript = "for (int i=0; i < ctx._source.elements.length; i++) {if (ctx._source.elements[i].SerialNumber == params.sn) { ctx._source.elements[i] = params.newElement; }}";

    String json = "{\"OUI\":\"00D09E\", \"SerialNumber\":\"NEW00000001AB\"}";
    Map<String, Object> jsonMap = new ObjectMapper().readValue(json, HashMap.class);
    Map<String, Object> params = ImmutableMap.of(
    	"sn", "00000001AA"
    	"newElement", jsonMap);

    UpdateRequest request = new UpdateRequest().index("PlayerIndex").id("1");
                request.script(new Script(ScriptType.INLINE,
                        "painless",
                        updateElementScript,   
                        params));

    client.update(request, RequestOptions.DEFAULT); // RestHighLevelClient

```

---

_[View the full topic](https://discuss.elastic.co/t/error-when-updating-an-array-element-object-using-java-rest-client-api/253835)._
