# How to use json in update script

**URL:** <https://discuss.elastic.co/t/how-to-use-json-in-update-script/21277>\
**Category:** Elasticsearch\
**Created:** [December 16, 2014, 3:04pm UTC](https://discuss.elastic.co/t/how-to-use-json-in-update-script/21277 "2014-12-16T15:04:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@roger](https://discuss.elastic.co/u/roger)\
**Post date:** [December 16, 2014, 3:04pm UTC](https://discuss.elastic.co/t/how-to-use-json-in-update-script/21277/1 "2014-12-16T15:04:43Z")

</div>

Hello

I'm trying to update a document whose root object contains a list of nested  
objects. I need to send an object of the nested type as a script parameter  
to append to the list

How can I append the json (a string type) to the nested objects list of the  
root object using Groovy? or should I use another script lang?

I tried using JsonSlurper [http://groovy-lang.org/json.html](http://groovy-lang.org/json.html) in Groovy,  
that converts between json and Groovy objects, but I always get:

Caused by:

> org.elasticsearch.script.groovy.GroovyScriptCompilationException:  
> MultipleCompilationErrorsException[startup failed:  
> Script3.groovy: 2: unable to resolve class JsonSlurper  
> @ line 2, column 19.  
> def jsonSlurper = new JsonSlurper();  
> ^  
> 1 error  
> ]  
> at  
> org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:117)  
> at  
> org.elasticsearch.script.ScriptService.getCompiledScript(ScriptService.java:368)  
> at org.elasticsearch.script.ScriptService.compile(ScriptService.java:354)  
> at  
> org.elasticsearch.script.ScriptService.executable(ScriptService.java:497)  
> at  
> org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:149)  
> ... 8 more

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAJp2531Qm2GZbvM7CMZSd8sqjUF-VQ%3DN6YUKQam5EOPd9pBvRA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJp2531Qm2GZbvM7CMZSd8sqjUF-VQ%3DN6YUKQam5EOPd9pBvRA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@roger](https://discuss.elastic.co/u/roger)\
**Post date:** [December 16, 2014, 5:17pm UTC](https://discuss.elastic.co/t/how-to-use-json-in-update-script/21277/2 "2014-12-16T17:17:04Z")

</div>

Ok, I found out that I can send a JSON as a script parameter and just  
append it to the nested objects list (with list += newObject or  
list.add(newObject) ) using groovy and it works

But it is not working with the Java API, I can only get it to work using  
the REST API.

When using Java the JSON is treated as a string, then I get the error:

object mapping [objectsList] trying to serialize a value with no field

> associated with it, current value [{"field":"value"}]

I can reproduce the error in the REST API by wrapping the JSON parameter  
with quotes:

_Works (using REST API):_

{

> "script": "ctx.\_source.objectsList += newObject",  
> "params": {  
> "newObject": {"field": "value"}  
> },  
> "lang": "groovy"  
> }

_Does not work (using REST API):_

{

> "script": "ctx.\_source.objectsList += newObject",  
> "params": {  
> "newObject": "{"field": "value"}"  
> },  
> "lang": "groovy"  
> }

_Does not work (using JAVA API):_

String script = "ctx.\_source.objectsList += newObject";

2014-12-16 13:04 GMT-02:00 Roger de Cordova Farias \<  
roger.farias@fontec.inf.br\>:

> Hello
> 
> I'm trying to update a document whose root object contains a list of  
> nested objects. I need to send an object of the nested type as a script  
> parameter to append to the list
> 
> How can I append the json (a string type) to the nested objects list of  
> the root object using Groovy? or should I use another script lang?
> 
> I tried using JsonSlurper [http://groovy-lang.org/json.html](http://groovy-lang.org/json.html) in Groovy,  
> that converts between json and Groovy objects, but I always get:
> 
> Caused by:
> 
> > org.elasticsearch.script.groovy.GroovyScriptCompilationException:  
> > MultipleCompilationErrorsException[startup failed:  
> > Script3.groovy: 2: unable to resolve class JsonSlurper  
> > @ line 2, column 19.  
> > def jsonSlurper = new JsonSlurper();  
> > ^  
> > 1 error  
> > ]  
> > at  
> > org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:117)  
> > at  
> > org.elasticsearch.script.ScriptService.getCompiledScript(ScriptService.java:368)  
> > at org.elasticsearch.script.ScriptService.compile(ScriptService.java:354)  
> > at  
> > org.elasticsearch.script.ScriptService.executable(ScriptService.java:497)  
> > at  
> > org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:149)  
> > ... 8 more

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAJp2531fsDup%2B0%3DtSR48ugsVkphLG%2B1s4QbOjLP7GjrMncBbTA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJp2531fsDup%2B0%3DtSR48ugsVkphLG%2B1s4QbOjLP7GjrMncBbTA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@roger](https://discuss.elastic.co/u/roger)\
**Post date:** [December 16, 2014, 5:19pm UTC](https://discuss.elastic.co/t/how-to-use-json-in-update-script/21277/3 "2014-12-16T17:19:56Z")

</div>

_Does not work (using JAVA API):_

String script = "ctx.\_source.objectsList += newObject";

> UpdateRequestBuilder prepareUpdate = client.prepareUpdate("indexName",  
> "typeName", "id");  
> prepareUpdate.setScriptLang("groovy");  
> prepareUpdate.setScript(script, ScriptType.INLINE);  
> prepareUpdate.addScriptParam("newObject", "{"status":"aasdsd"}");  
> prepareUpdate.get();

Is there a way to reproduce the working REST API behavior with the Java  
API?

2014-12-16 15:17 GMT-02:00 Roger de Cordova Farias \<  
roger.farias@fontec.inf.br\>:

> Ok, I found out that I can send a JSON as a script parameter and just  
> append it to the nested objects list (with list += newObject or  
> list.add(newObject) ) using groovy and it works
> 
> But it is not working with the Java API, I can only get it to work using  
> the REST API.
> 
> When using Java the JSON is treated as a string, then I get the error:
> 
> object mapping [objectsList] trying to serialize a value with no field
> 
> > associated with it, current value [{"field":"value"}]
> 
> I can reproduce the error in the REST API by wrapping the JSON parameter  
> with quotes:
> 
> _Works (using REST API):_
> 
> {
> 
> > "script": "ctx.\_source.objectsList += newObject",  
> > "params": {  
> > "newObject": {"field": "value"}  
> > },  
> > "lang": "groovy"  
> > }
> 
> _Does not work (using REST API):_
> 
> {
> 
> > "script": "ctx.\_source.objectsList += newObject",  
> > "params": {  
> > "newObject": "{"field": "value"}"  
> > },  
> > "lang": "groovy"  
> > }
> 
> _Does not work (using JAVA API):_
> 
> String script = "ctx.\_source.objectsList += newObject";
> 
> 2014-12-16 13:04 GMT-02:00 Roger de Cordova Farias \<  
> roger.farias@fontec.inf.br\>:
> 
> > Hello
> > 
> > I'm trying to update a document whose root object contains a list of  
> > nested objects. I need to send an object of the nested type as a script  
> > parameter to append to the list
> > 
> > How can I append the json (a string type) to the nested objects list of  
> > the root object using Groovy? or should I use another script lang?
> > 
> > I tried using JsonSlurper [http://groovy-lang.org/json.html](http://groovy-lang.org/json.html) in Groovy,  
> > that converts between json and Groovy objects, but I always get:
> > 
> > Caused by:
> > 
> > > org.elasticsearch.script.groovy.GroovyScriptCompilationException:  
> > > MultipleCompilationErrorsException[startup failed:  
> > > Script3.groovy: 2: unable to resolve class JsonSlurper  
> > > @ line 2, column 19.  
> > > def jsonSlurper = new JsonSlurper();  
> > > ^  
> > > 1 error  
> > > ]  
> > > at  
> > > org.elasticsearch.script.groovy.GroovyScriptEngineService.compile(GroovyScriptEngineService.java:117)  
> > > at  
> > > org.elasticsearch.script.ScriptService.getCompiledScript(ScriptService.java:368)  
> > > at org.elasticsearch.script.ScriptService.compile(ScriptService.java:354)  
> > > at  
> > > org.elasticsearch.script.ScriptService.executable(ScriptService.java:497)  
> > > at  
> > > org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:149)  
> > > ... 8 more

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAJp2530t5o9jcpgGRsJo1zV%2BaSvD7Uk8QyTKha6VR-RoHQuqsQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJp2530t5o9jcpgGRsJo1zV%2BaSvD7Uk8QyTKha6VR-RoHQuqsQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:32pm UTC](https://discuss.elastic.co/t/how-to-use-json-in-update-script/21277/4 "2016-03-01T17:32:15Z")

</div>

See [this](https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135)

---

<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-use-json-in-update-script/21277/5 "2017-07-05T23:12:23Z")

</div>


