# Multiple foreach processors on array of JSON

**URL:** <https://discuss.elastic.co/t/multiple-foreach-processors-on-array-of-json/99728>\
**Category:** Elasticsearch\
**Created:** [September 7, 2017, 1:51pm UTC](https://discuss.elastic.co/t/multiple-foreach-processors-on-array-of-json/99728 "2017-09-07T13:51:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [September 7, 2017, 1:51pm UTC](https://discuss.elastic.co/t/multiple-foreach-processors-on-array-of-json/99728/1 "2017-09-07T13:51:58Z")

</div>

In the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/foreach-processor.html) for the `foreach` processor, it says "The foreach processor does restore the original value, so that value is available to processors after the foreach processor." Can someone clarify what this means?

I have this pipeline that works, but based on my reading of the documentation I'm not actually expecting it to work:

```auto
{
  "pipeline": {
    "processors": [
      {
        "foreach": {
          "field": "venues",
          "processor": {
            "uppercase": {
              "field": "_ingest._value.venue"
            }
          }
        }
      },
      {
        "foreach": {
          "field": "venues",
          "processor": {
            "gsub": {
              "field": "_ingest._value.venue",
              "pattern": "CHI",
              "replacement": "XXX"
            }
          }
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_type": "type",
      "_id": "id",
      "_source": {
        "body": "blah blah",
        "venues": [
          {
            "id": "1",
            "venue": "Chicago, IL"
          },
          {
            "id": "2",
            "venue": "Washington, D.C."
          }
        ]
      }
    }
  ]
}

```

In the second `foreach`, shouldn't `_ingest._value.venue` be the original value and not the value that got uppercased in the prior `foreach`?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 11, 2017, 7:40am UTC](https://discuss.elastic.co/t/multiple-foreach-processors-on-array-of-json/99728/2 "2017-09-11T07:40:09Z")

</div>

Hey Loren,

the way I understand it (after reading it 5 times in a row 🙂 , it works as expected. I think what it is trying to tell is that if you set `_ingest._value` outside a `foreach` loop, this value is gonna be restored. For example setting this first and then running your loops, should still work.

```auto
      {
        "set": {
          "field": "_ingest._value",
          "value": "foo"
        }
      }

```

--Alex

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [September 11, 2017, 11:31am UTC](https://discuss.elastic.co/t/multiple-foreach-processors-on-array-of-json/99728/3 "2017-09-11T11:31:28Z")

</div>

Thank you @spinscale. Good to know I wasn't the only person scratching my head over it.

---

<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:** [October 9, 2017, 11:31am UTC](https://discuss.elastic.co/t/multiple-foreach-processors-on-array-of-json/99728/4 "2017-10-09T11:31:45Z")

</div>

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