# Loop over a map in Painless

**URL:** <https://discuss.elastic.co/t/loop-over-a-map-in-painless/70308>\
**Category:** Elasticsearch\
**Created:** [December 31, 2016, 2:03am UTC](https://discuss.elastic.co/t/loop-over-a-map-in-painless/70308 "2016-12-31T02:03:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [December 31, 2016, 2:03am UTC](https://discuss.elastic.co/t/loop-over-a-map-in-painless/70308/1 "2016-12-31T02:03:56Z")

</div>

I would like to loop over the entries in the map using the Painless script for updating by query...

"script": {  
"inline" : "for (int i = 0; i \< ctx.\_source['Items'].length; ++i) { if (ctx.\_source['Items'][i].id == [params.id](http://params.id)) { for (entry : params.changes.entrySet()) { ctx.\_source['Items'][i][entry.getKey()] = entry.getValue() } } }",  
"lang" : "painless",  
"params": {  
"id": "a7f168ce207080641b87098708433c4b",  
"changes" : {  
"Key0": "val00",  
"Key1": "val01",  
"Key2": "val02",  
"Key3": "val03",  
}  
}  
}

Does not seem to work though. Get error like:

**"reason" : "invalid sequence of tokens near [':'].",**

Does not seem to like the looping part it looks like

Can 'params' be treated as a Java HashMap? Can 'params' have nested objects within.

---

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [December 31, 2016, 2:15am UTC](https://discuss.elastic.co/t/loop-over-a-map-in-painless/70308/2 "2016-12-31T02:15:47Z")

</div>

Soon after I posted I found the issue. It was complaining about ':' in the loop, rightly so I guess in an overall json doc. Replaced it with 'in' and it worked.

for (entry **in** params.changes.entrySet())

is the correct syntax to use

---

<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:** [January 28, 2017, 2:15am UTC](https://discuss.elastic.co/t/loop-over-a-map-in-painless/70308/3 "2017-01-28T02:15:56Z")

</div>

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