Usando o analisador de linha de comando JSON jq
:
$ jq -r '.reports[] |
"\(.rowName) :",
"books -> \(.books)",
"toys -> \(.toys)",
"games -> \(.games)\n"' input.json
CWQ:
books -> 3
toys -> 0
games -> 3
AED:
books -> 0
toys -> 0
games -> 0
Observação: isso usa a interpolação de string de jq
e repete todas as entradas na matriz reports
, escolhendo as informações relevantes.
Atualização: manipulando .books
como uma matriz de objetos:
Dado
{
"reports": [
{
"games": 3,
"toys": 0,
"books": [
{
"provider": "The provider",
"name": "The name"
}
],
"to": "2017-07-22T00:00:00.000+0000",
"from": "2017-07-21T00:00:00.000+0000",
"eventType": "bookedIn",
"rowName": "CWQ"
},
{
"games": 0,
"toys": 0,
"books": [
{
"provider": "The 1st provider",
"name": "The 1st name"
},
{
"provider": "The 2nd provider",
"name": "The 2nd name"
}
],
"to": "2017-07-22T00:00:00.000+0000",
"from": "2017-07-21T00:00:00.000+0000",
"eventType": "bookedOut",
"rowName": "AED"
}
]
}
Podemos simplesmente fazer
$ jq -r '.reports[] |
"\(.rowName):",
( .books[] | "book name -> \(.name)",
"book provider -> \(.provider)" ),
"toys -> \(.toys)",
"games -> \(.games)\n"'
CWQ:
book name -> The name
book provider -> The provider
toys -> 0
games -> 3
AED:
book name -> The 1st name
book provider -> The 1st provider
book name -> The 2nd name
book provider -> The 2nd provider
toys -> 0
games -> 0