# Best way to update object inside array

**URL:** <https://discuss.elastic.co/t/best-way-to-update-object-inside-array/133755>\
**Category:** Elasticsearch\
**Created:** [May 29, 2018, 8:54pm UTC](https://discuss.elastic.co/t/best-way-to-update-object-inside-array/133755 "2018-05-29T20:54:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Blaustein](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Martin\_Blaustein](https://discuss.elastic.co/u/Martin_Blaustein)\
**Post date:** [May 29, 2018, 8:54pm UTC](https://discuss.elastic.co/t/best-way-to-update-object-inside-array/133755/1 "2018-05-29T20:54:56Z")

</div>

Hi, I'm trying to update an object inside an  
{  
...  
arrayKey: [{  
key1: value1,  
key2: value2  
}  
]  
...  
}

The objective is to change the value of **key1**. The only way I could find to do it, is to replace the entire object with the following:

```
   String objectValue = new ObjectMapper().valueToTree(fieldValue.get(0)).toString();
        XContentParser parser = JsonXContent.jsonXContent
                .createParser(NamedXContentRegistry.EMPTY,objectValue);
        contentBuilder.startArray(fieldName);
        contentBuilder.copyCurrentStructure(parser);
        contentBuilder.endArray();

```

Is there a better way? Thanks!

---

<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:** [June 26, 2018, 8:54pm UTC](https://discuss.elastic.co/t/best-way-to-update-object-inside-array/133755/2 "2018-06-26T20:54:59Z")

</div>

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