简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM