Search code examples
jsonrubylogstashlogstash-groklogstash-jdbc

Nested json objects in logstash aggregate filter plugin


I'm using logstash aggregate filter plugin to insert data to ES.

I want to create a json like

"Countries" : {
        "Asia" : {
            "name" : "Srilanka"
        },
        "Africa" : {
            "name" : "Kenya"
        }
    }

when uploaded to ES.

I have tried

map['Countries'] = {
        map['Asia'] =  {
            'name' => event.get('name_Asia')
        },
        map['Africa'] =  {
            'name' => event.get('name_Africa')
        }
}

But it doesn't work.

Is it possible to make create above json?


Solution

  • In the first place to produce nested hashes, you should use hashrockets => not assignments inside a hash. One might create this hash in one turn:

    map = { 
      'Countries' => {
        'Asia' =>  {
          'name' => event.get('name_Asia')
        },
        'Africa' => {
          'name' => event.get('name_Africa')
        }
      }
    }
    

    Then you can produce JSON out of it with JSON.dump

    require 'json'
    JSON.dump(map)