# Iterate over Object keys

**URL:** <https://discuss.elastic.co/t/iterate-over-object-keys/168026>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 12, 2019, 12:18pm UTC](https://discuss.elastic.co/t/iterate-over-object-keys/168026 "2019-02-12T12:18:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyclohexan](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cyclohexan](https://discuss.elastic.co/u/cyclohexan)\
**Post date:** [February 12, 2019, 12:18pm UTC](https://discuss.elastic.co/t/iterate-over-object-keys/168026/1 "2019-02-12T12:18:37Z")

</div>

I have a painless script inside of an update query.  
The Script receives a parameter containing an array with different hashes like this:

```
{
	"fields": [
		{"keyA": 1, "keyB": 2, "keyC": "something"},
		{"keyD": "anything", "keyE": 2},
		{"name": 1, "age": 2, "city": "London"}
	]
}

```

I need to iterate over the keys of each of these objects in painless.  
Therefore I need to get the keys that the object has.

```
for (int i=0; i<params.fields.length; i++) {
	Object obj = params.fields[i];
	
	// iterate over object keys
	for (...) {
		// do something with the object key
	}
}

```

In Javascript my inner loop would look something like this:

```
for (let [key, value] of Object.entries(obj)) {
	// do something with the object key
}

```

But how can this be done in painless?

---

<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:** [March 12, 2019, 12:18pm UTC](https://discuss.elastic.co/t/iterate-over-object-keys/168026/2 "2019-03-12T12:18:38Z")

</div>

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