JQ if / elif / else example

Branch on values in jq with if / elif / else / end. Interactive example that maps input values to different results using conditional logic.

The jq expression

def areaFromCountry: if . == "USA" then "North America"
  elif . == "Germany" then "Europe"
  elif . == "Japan" then "Asia"
  else "Unknown"
  end;

.countries[] | {area: .name | areaFromCountry}

jq conditionals use the form if COND then A elif COND2 then B else C end, where each branch is itself an expression. Here we define areaFromCountry, which maps a country name to a continent, and apply it to each country to derive its area, falling back to "Unknown" for unmatched values.