arquivo json de entrada:
[
{
"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"
}
]
Eu quero estender o seguinte filtro:
$ 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
para se livrar de todos os "ABC padrão" da lista de grupos separados por | na saída
Eu tentei com if adicional, mas ele falha com erro:
$ 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:
agradeço qualquer dica...
a saída esperada para o json de amostra é:
1e9b2215-9efd-67cc-9113-1e21c74f09d3 u0159 n/a
1e9b2215-9efd-67cc-9113-2b21c74f09d3 u0154 AGE
1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f u0155 ZZZ|NTE
Atualização: Solução fornecida pela 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
Supondo que, se
groups
existir, é uma matriz, você pode filtrar os indesejadosname
usandoselect
: