My Json field looks like below,
{"group" : [{"name":"group1","tag":[{"name":"tag1"},{"name":"tag2"},{"name":"tag3"},{"name":"tag4"}]}],"association" : }
And my config file having mutate filter is as follows,
mutate{
add_field => {
"group_name" => "%{[group][name]}"
"tag1" => "%{[group][tag][0][name]}"
"tag2" => "%{[group][tag][1][name]}"
"tag3" => "%{[group][tag][2][name]}"
"tag4" => "%{[group][tag][4][name]}"
}
}
This code works fine . However, there are a varying number of values in the "name" field. Ie, some json doesn't have 4 set of name:tagn inside tag and some have additional 5 or 6 set of name:tagn inside tag field. However, for each name, there will definitely be a corresponding tagn in the "value" field.
So this makes me difficult to automate ingestion and everytime I have to make changes in config file.
I heard we can solve it using ruby code. But I am a starter and I do not know much about ruby.
Can anyone help me iterating this based on number of sets of name:tagn available inside tag array using ruby.
HI,
Thanks for the reply.
I think this will help me iterating. When I am iterating I have to ingest these values to a particular meta.
Currently I tried something like below,
mutate{
add_field => {
"group_name" => "%{[group][name]}"
"test" => "%{[group][tag]}"
"tags" => ""
"tag" => "%{[group][tag][0][name]}"
}
}
ruby {
code => '
a = event.get("[group][0][tag]")
a.each_index { |x|
event.set("tag#{x+1}", a["name"])
}
'
}
And my result is
{
"group_name" => "group1",
"test" => "{name=tag1},{name=tag2},{name=tag3},{name=tag4},
"tag" => "tag1",
"tags" => [
[0] "",
[1] "_rubyexception"
]
}
How can I overcome this and put these values to a particular meta.
Hello @Badger,
Sorry that I could not edit the previous post.
The ruby exception is as below, ERROR][logstash.filters.ruby ][test] Ruby exception occurred: undefined method each_index' for nil:NilClass
But since I have given prune and fingerprint filters it is filtering the tag1,tag2... fields. Can I specify something common there like tag* or something which will take all the field starting with tag.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.