# Filtering json fields to be outputted by logstash

**URL:** <https://discuss.elastic.co/t/filtering-json-fields-to-be-outputted-by-logstash/92253>\
**Category:** Logstash\
**Created:** [July 7, 2017, 11:05am UTC](https://discuss.elastic.co/t/filtering-json-fields-to-be-outputted-by-logstash/92253 "2017-07-07T11:05:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aszac](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@aszac](https://discuss.elastic.co/u/aszac)\
**Post date:** [July 7, 2017, 11:05am UTC](https://discuss.elastic.co/t/filtering-json-fields-to-be-outputted-by-logstash/92253/1 "2017-07-07T11:05:28Z")

</div>

Hello!

I am using logstash 5.4 to read a json interaction from kafka and then filter it so that:

1. interaction will be dropped unless field [car.name](http://car.name) exist
2. only fields [car.name](http://car.name) and car.model are outputted

My config looks like this:

input {  
kafka {  
topics =\> members  
bootstrap\_servers =\> "\*\*\*"  
codec =\> "json"  
group\_id =\> "members"  
}  
}  
filter {  
if ![car][name] {  
drop { }  
}  
prune {  
whitelist\_names =\> [  
"[car][name]",  
"[car][model]"  
]  
}  
}  
output {  
stdout {  
codec =\> "json"  
}  
}

Input and output example:  
{"name":"John","surname":"Smith","age":30,"car":{"name":"BMW","model":"320","speed":123},"job":{"company":"Tesco","title":"manager"}}  
{"@timestamp":"2017-07-07T11:00:57.986Z","car":{"name":"BMW","model":"320","speed":123},"surname":"Smith","name":"John"}

How do I make the config correct, so that only the whitelisted fields are being outputted?

---

<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:** [July 10, 2017, 10:52am UTC](https://discuss.elastic.co/t/filtering-json-fields-to-be-outputted-by-logstash/92253/2 "2017-07-10T10:52:53Z")

</div>

Unfortunately the prune filter doesn't support subfields. For now you'll have to use a ruby filter to prune the subfields of the `car` field.

> <https://github.com/logstash-plugins/logstash-filter-prune/issues/12>

---

<div class="post-metadata">

**Author:** ![aszac](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@aszac](https://discuss.elastic.co/u/aszac)\
**Post date:** [July 12, 2017, 1:38pm UTC](https://discuss.elastic.co/t/filtering-json-fields-to-be-outputted-by-logstash/92253/3 "2017-07-12T13:38:17Z")

</div>

I have got around that problem by first using the mutate -\> add\_field plugin to flatten the json and then used the prune filter on the new flattened field.

e.g.  
mutate {  
add\_field =\> {  
"[car.name](http://car.name)" =\> "%{[car][name]}"  
"car.model" =\> "%{[car][model]}"  
}  
}  
prune {  
whitelist\_names =\> [  
"[car.name](http://car.name)",  
"car.model",  
]  
}

---

<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:** [August 9, 2017, 1:38pm UTC](https://discuss.elastic.co/t/filtering-json-fields-to-be-outputted-by-logstash/92253/4 "2017-08-09T13:38:46Z")

</div>

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