7

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?

18

use the alternative operator: //

so :

$jq -r '.|[.login, .lastLoginTime // "-" , .lastLoginFrom // "-" ]|@tsv' test_json
050111  1529730115000   192.168.66.230
050112  -   -
  • 2
    The initial .| is not needed. – Kusalananda Jun 23 '18 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.