Can you provide more context? Logstash does not drop field unless you have a mutate with the remove_field telling it to remove the field.
If the value is nil, it will output as nil.
Also, your code will only work for top level fields, it is a similar issue as your previous question.
You probably could rework that code to do what you want.