Jq – print “-” for null values

jqjson

input json:

{
  "id": "3885",
  "login": "050111",
  "lastLoginTime": 1529730115000,
  "lastLoginFrom": "192.168.66.230"
}
{
  "id": "3898",
  "login": "050112",
  "lastLoginTime": null,
  "lastLoginFrom": null
}

I want to get output for login, lastLoginTime and lastLoginFrom in tabulator delimited format:

050111  1529730115000   192.168.66.230
050112              -                -

with below jq filter I get on output no "null" values which I could replace with "-"

$ jq -r '.|[.login, .lastLoginTime, .lastLoginFrom]|@tsv' test_json
050111  1529730115000   192.168.66.230
050112

Is there any other way to get "-" printed for such null values?

Best Answer

use the alternative operator: //

so :

$jq -r '.|[.login, .lastLoginTime // "-" , .lastLoginFrom // "-" ]|@tsv' test_json
050111  1529730115000   192.168.66.230
050112  -   -
Related Question