Search code examples
velocity

Velocity check if Map has a key with null value


I'm creating a velocity template to which I pass a map, and I want to get only the keys with values.

It is my code:

#set ($value = $map.get($key))
  #if($value != "")
    "$key": "$value",
  #end
  • map.get(parameter) != null
  • map.get(parameter) != ''

It prints: $value


Solution

  • Just try:

    #if($value)
      "$key": "$value",
    #end
    

    It will filter out null and empty values.