# Java API UpdateArray

**URL:** <https://discuss.elastic.co/t/java-api-updatearray/88898>\
**Category:** Elasticsearch\
**Created:** [June 10, 2017, 12:00am UTC](https://discuss.elastic.co/t/java-api-updatearray/88898 "2017-06-10T00:00:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skolty](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@Skolty](https://discuss.elastic.co/u/Skolty)\
**Post date:** [June 10, 2017, 12:00am UTC](https://discuss.elastic.co/t/java-api-updatearray/88898/1 "2017-06-10T00:00:36Z")

</div>

Hi Guys !

I have a document which contains an array of JSONObject.

When I add a JSONObject in my array from Sense like this, it works fine :

[quote] POST index/type/1/\_update  
{  
"script" : {  
"inline": "ctx.\_source.people.add(params.person)",  
"lang": "painless",  
"params" : {  
"person" : {  
"field" : "test"  
}  
}  
}  
}  
[/quote]

But when I'm trying to add throw the Java Api with this code :

> [@](#):
>
> Map\<String, Object\> params = new HashMap\<\>();  
> params.put("person", valueTest);  
> Script scriptTest = new Script(ScriptType.INLINE, "painless", "ctx.\_source.people.add(params.person);", params);  
> UpdateRequest bulkRequest = new UpdateRequest("index","type",id)  
> .script(scriptTest);  
> client.prepareUpdate("cityflow9","traject",id).setScript(scriptTest).execute().get();

I got the following error : java.io.IOException: can not write type [class org.json.JSONObject]

I tried to replace the JSONObject with a XContentBuilder but the same exception is throwing...

Can anyone has an explenation or an idea how I can fix this ?

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [June 10, 2017, 12:16am UTC](https://discuss.elastic.co/t/java-api-updatearray/88898/2 "2017-06-10T00:16:18Z")

</div>

Can you show the code snippet that creates `valueTest`?

---

<div class="post-metadata">

**Author:** ![Skolty](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@Skolty](https://discuss.elastic.co/u/Skolty)\
**Post date:** [June 10, 2017, 7:10am UTC](https://discuss.elastic.co/t/java-api-updatearray/88898/3 "2017-06-10T07:10:24Z")

</div>

Hello,

Thanks for replying !

Yes, of course, I try this :

> [@](#):
>
> ```
> JSONObject valueTest = new JSONObject();
> valueTest.put("macAdresse", "Testing");
> valueTest.put("duration", 123);
> 
> ```

And I try this as well :

> [@](#):
>
> XContentBuilder valuetest = jsonBuilder()  
> .startObject()  
> .field("macAdresse", "Testing")  
> .field("duration", 123)  
> .endObject();

Both give me the error ☹

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [June 10, 2017, 1:54pm UTC](https://discuss.elastic.co/t/java-api-updatearray/88898/4 "2017-06-10T13:54:10Z")

</div>

Try with a `HashMap`

---

<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 8, 2017, 1:54pm UTC](https://discuss.elastic.co/t/java-api-updatearray/88898/5 "2017-07-08T13:54:17Z")

</div>

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