# 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:** 3
**Page:** 1

<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, 3:17pm UTC](https://discuss.elastic.co/t/error-when-updating-an-array-element-object-using-java-rest-client-api/253835/1 "2020-10-30T15:17:06Z")

</div>

I am trying to update an array element in the document. The update should replace an existing element.

This is what the document looks like

```auto
    "first" : "johnny",
    "last" : "gaudreau",
    "elements" : [
    {
       "OUI" : "AAAAAA",
       "SerialNumber" : "00000001AA"
    },
    {
       "OUI" : "00D09E",
       "SerialNumber" : "00000001B"
    },
    {
       "OUI" : "00D09E",
       "SerialNumber" : "00000001C"
    }
  ]

```

I can update the element object with the following **curl command** using the script option

```auto
    curl -X POST "localhost:9200/PlayerIndex/_update/1?pretty" -H 'Content-Type: application/json' -d'
    {
      "script": {
        "lang": "painless",
        "source": "for (int i=0; i < ctx._source.elements.length; i++) {if (ctx._source.elements[i].SerialNumber == params.sn) { ctx._source.elements[i] = params.newElement }}",
        "params": {
          "sn": "00000001AA",
          "newElement": {"OUI":"00D09E", "SerialNumber":"00000001AB"}
        }
      }
    }
    '

```

However, when I try to do the same in Java with the following code I get the **following exception**

> [ElasticsearchException[Elasticsearch exception [type=mapper\_parsing\_exception, reason=object mapping for [element] tried to parse field [null] as object, but found a concrete value]]]

Here is the **Java code**

```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; }}";

    Map<String, Object> params = new HashMap<>();
    params.put("sn", "00000001AA");
    params.put("newElement", "{\"OUI\":\"00D09E\", \"SerialNumber\":\"00000001AB\"}");

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

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

```

I have verified that I can update the field in the element object (e.g. the OUI field). But I want to update the whole element object. It seems like the Java code does not see the "newElement" as an object but a value. What am I doing wrong?

---

<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

```

---

<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: [November 27, 2020, 7:44pm UTC](https://discuss.elastic.co/t/error-when-updating-an-array-element-object-using-java-rest-client-api/253835/3 "2020-11-27T19:44:57Z")

</div>

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