# Array of objects - LogStash Conf

**URL:** https://discuss.elastic.co/t/array-of-objects-logstash-conf/280750
**Category:** Logstash
**Created:** [August 8, 2021, 9:31pm UTC](https://discuss.elastic.co/t/array-of-objects-logstash-conf/280750 "2021-08-08T21:31:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![me.mohammed](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@me.mohammed](https://discuss.elastic.co/u/me.mohammed)
#### Post date: [August 8, 2021, 9:31pm UTC](https://discuss.elastic.co/t/array-of-objects-logstash-conf/280750/1 "2021-08-08T21:31:06Z")

</div>

I'm currently working on fetching my data from MySQL and pushing it to ElasticSearch through LogStash, although I'm kind of having an issue with the logstash configuration

I basically have Products and each of those products can have a (BOGO - BUY ONE GET ONE) Deal, they are stored separately in their own tables & linked by a ONE -\> MANY) relation.

To make things easier for me, I basically have a SQL View that joins everything for me in-order to easily create a JSON Object.

Since products and BOGO deals have a (ONE -\> MANY) relation, a SELECT query using that view would return the product multiple times, each with a different BOGO deal.

What I want to do is collect all of those BOGO deals and place them in an array of objects.

Note that I'm trying to end up with a JSON Object that would look like this

```auto
{
  "id": "someId",
  "createdAt": "Date",
  "modifiedAt": "Date",

  "manufacturerCompany": {
    "id": "someId",
    "name": "someName",
    "createdAt": "Date",
    "modifiedAt": "Date"
  },

  "bogoDeals": [
    {JSON Object}, {Json Object}, {Json OBJECT}
  ]
}

```

My current code looks similar to this, note that it does not work

logstash.conf - Filter section only

```auto
filter {
filter {
  mutate {
    #Product Manufacturing Company
    rename => {"manufacturer_company_id" => "[manufacturerCompany][id]"}
    rename => {"manufacturer_company_name" => "[manufacturerCompany][name]"}
    rename => {"manufacturer_company_created_at" => "[manufacturerCompany][createdAt]"}
    rename => {"manufacturer_company_modified_at" => "[manufacturerCompany][modifiedAt]"}

    #Product
    rename => {"product_id" => "[id]"}
    rename => {"product_created_at" => "[createdAt]"}
    rename => {"product_modified_at" => "[modifiedAt]"}
  }

  
  aggregate {
    task_id => "%{product_id}"
    code => "

    map['manufacturerCompany'] ||= []
    map['manufacturerCompany'] << {
      'id' => event.get('[manufacturerCompany][id]'),
      'name' => event.get('[manufacturerCompany][name]'),
      'createdAt' => event.get('[manufacturerCompany][createdAt]'),
      'modifiedAt' => event.get('[manufacturerCompany][modifiedAt]')
    }

    map['bogoDeals'] ||= []
    map['bogoDeals'] << {
      'id' => event.get('bogo_deal_id'),
      'createdAt' => event.get('bogo_deal_created_at'),
      'modifiedAt' => event.get('bogo_deal_modified_at')
      }

    event.cancel()
    "

    push_previous_map_as_event => true
    push_map_as_event_on_timeout => true
    inactivity_timeout => 5
    timeout => 10
  }

    mutate {
    remove_field => ["@version", "unix_ts_in_secs", "@timestamp", "tags"]
  }

  ruby {
    path => "logstash-ruby.rb"
  }
}

```

```auto
def filter(event)

  @bogoDeals = event.get('bogoDeals')
  
  puts @bogoDeals.inspect
  if @bogoDeals
    print("executing")
    event.set('bogoDeals', @bogoDeals.uniq{|p| p["id"]})
    print("executed")
  end

  return [event]
end

```

For some reason all the bogoDeals are all null in ruby, is my way of doing this correct? if so what am I doing wrong here and why are the bogoDeals null in ruby, not that I have absolutely no experience in ruby, much appreciated.

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [August 8, 2021, 10:08pm UTC](https://discuss.elastic.co/t/array-of-objects-logstash-conf/280750/2 "2021-08-08T22:08:31Z")

</div>

> [@me.mohammed](#):
>
> `task_id => "%{product_id}"`

You have already renamed product\_id to id, so this will not do what you want. Also, you are creating map['manufacturerCompany'] as an array, which is not what it looks like in the desired output.

---

<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: [September 5, 2021, 10:08pm UTC](https://discuss.elastic.co/t/array-of-objects-logstash-conf/280750/3 "2021-09-05T22:08:34Z")

</div>

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