1

input json file:

[
  {
    "id": "1e9b2215-9efd-67cc-9113-1e21c74f09d3",
    "login": "u0159",
    "name": "Pat ABC",
    "role": "User",
    "groups": null,
    "disabled": false,
    "lLoginTime": 1567158950468,
    "lLoginFrom": "192.168.100.11"
  },
  {
    "id": "1e9b2215-9efd-67cc-9113-2b21c74f09d3",
    "login": "u0154",
    "name": "Roman ABC",
    "role": "User",
    "groups": [
      {
        "id": "1e8dc82a-596f-623c-8bc5-100000000000",
        "name": "Default ABC"
      },
      {
        "id": "1e99833f-9876-65d0-9d24-237228b9d9e0",
        "name": "AGE"
      }
    ],
    "disabled": false,
    "lLoginTime": 1567158950468,
    "lLoginFrom": "192.168.100.11"
  },
  {
    "id": "1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f",
    "login": "u0155",
    "name": "Tomas CDE",
    "role": "User",
    "groups": [
      {
        "id": "1e8dc82a-596f-623c-8bc5-100000000000",
        "name": "ZZZ"
      },
      {
        "id": "1e8dc82a-596f-623c-8bc5-100000000000",
        "name": "Default ABC"
      },
      {
        "id": "1e99234f-9876-65d0-9d24-534528b9d9e0",
        "name": "NTE"
      }
    ],
    "disabled": false,
    "lLoginTime": 1566302653093,
    "lLoginFrom": "192.168.100.16"
  }
]

I want to extend following filter:

$ jq -r '.[]|[.id, .login, ( if .groups == null then "n/a" else ([.groups[].name]|join("|")) end )]|@tsv' /tmp/test__
1e9b2215-9efd-67cc-9113-1e21c74f09d3    u0159   n/a
1e9b2215-9efd-67cc-9113-2b21c74f09d3    u0154   Default ABC|AGE
1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f    u0155   ZZZ|Default ABC|NTE

to rid of all "Default ABC" from the list of groups separated by | on output

I tried with additional if but it fails with error:

$ jq -r '.[]|[.id, .login, ( if .groups == null then "n/a" else ( if .groups[].name != "Default ABC" then [.groups[].name]|join("|")) end ) end )]|@tsv' /tmp/test__
jq: error: syntax error, unexpected ')' (Unix shell quoting issues?) at <top-level>, line 1:

appreciate any hint...

expected output for the sample json is:

1e9b2215-9efd-67cc-9113-1e21c74f09d3    u0159   n/a
1e9b2215-9efd-67cc-9113-2b21c74f09d3    u0154   AGE
1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f    u0155   ZZZ|NTE

Update: Solution provided by oliv:

$ jq -r '.[]|[.id,.login,if .groups then (.groups|map(select(.name != "Default ABC").name)|join("|")) else "n/a" end]|@tsv' /tmp/test__
1e9b2215-9efd-67cc-9113-1e21c74f09d3    u0159   n/a
1e9b2215-9efd-67cc-9113-2b21c74f09d3    u0154   AGE
1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f    u0155   ZZZ|NTE
2
  • I think you want elif but I can't make a fixed version because 1) every if-then-else must include the else branch, and I don't know what to put in it, and 2) I'm not sure what you actually want to do about "Default ABC", which would be the else case, anyway - at the moment all the items would be repeated once for each other item (try if .groups == null then "n/a" elif .groups[].name != "Default ABC" then [.groups[].name]|join("|") else "?????" end and see the output). – Michael Homer Sep 5 '19 at 7:33
  • I need to ignore all occurrences of "Default ABC". Of course I could process the first filter output via sed or tr to rid of "Default ABC", but I thought maybe it can be handled directly in jq filter... – Chris Sep 5 '19 at 7:43
2

Assuming that if groups exists, it's an array, you could filter out the unwanted name using select:

<file jq -r '.[]|[.id,.login,if .groups then (.groups|map(select(.name != "Default ABC").name)|join("|")) else "n/a" end]|@tsv'

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.