# Beginner question add new field based on field in array of json objects

**URL:** <https://discuss.elastic.co/t/beginner-question-add-new-field-based-on-field-in-array-of-json-objects/106256>\
**Category:** Logstash\
**Created:** [November 2, 2017, 9:54pm UTC](https://discuss.elastic.co/t/beginner-question-add-new-field-based-on-field-in-array-of-json-objects/106256 "2017-11-02T21:54:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrvnkmr](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shrvnkmr](https://discuss.elastic.co/u/shrvnkmr)\
**Post date:** [November 2, 2017, 9:54pm UTC](https://discuss.elastic.co/t/beginner-question-add-new-field-based-on-field-in-array-of-json-objects/106256/1 "2017-11-02T21:54:33Z")

</div>

We have input json objects looks like below

```
{
  "ids": [
    {
      "id": 1,
      "name": "name1"
    },
    {
      "id": 2,
      "name": "name2"
    }
  ],
  "codes": [
    {
      "code": "4"
    },
    {
      "code": "2"
    }
  ]
}

{
      "ids": [
        {
          "id": 1,
          "name": "name1"
        },
        {
          "id": 2,
          "name": "name2"
        }
      ],
      "codes": [
        {
          "code": "1"
        },
        {
          "code": "2"
        }
      ]
    }

```

I want to add a new field and set to "Y" to json object if "name1" exists in "ids" and "4" exists in "codes" else set it to "N".  
So i added in filter

```
if [ids][name] == "name1" and [codes][code] == "4" {
mutate{add_field=>{"new_field":"Y"}}
}else{
mutate{add_field=>{"new_field":"N"}}
}

```

but it always setting to "N"

Can someone help me out here?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 3, 2017, 7:09am UTC](https://discuss.elastic.co/t/beginner-question-add-new-field-based-on-field-in-array-of-json-objects/106256/2 "2017-11-03T07:09:04Z")

</div>

You'll have to use a ruby filter for this. Untested:

```ruby
if event.get('ids').find { |i| i['name'] == 'name1' } && event.get('codes').find { |i| i['code'] == '4' }
  event.set('new_field', 'Y')
else
  event.set('new_field', 'N')
end
}

```

---

<div class="post-metadata">

**Author:** ![shrvnkmr](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shrvnkmr](https://discuss.elastic.co/u/shrvnkmr)\
**Post date:** [November 3, 2017, 5:49pm UTC](https://discuss.elastic.co/t/beginner-question-add-new-field-based-on-field-in-array-of-json-objects/106256/3 "2017-11-03T17:49:51Z")

</div>

Thanks It Worked.

---

<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:** [December 1, 2017, 5:49pm UTC](https://discuss.elastic.co/t/beginner-question-add-new-field-based-on-field-in-array-of-json-objects/106256/4 "2017-12-01T17:49:55Z")

</div>

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