![Jq: Selecione a chave e o valor de um suporte](https://rvso.com/image/178463/Jq%3A%20Selecione%20a%20chave%20e%20o%20valor%20de%20um%20suporte.png)
Eu tenho um arquivo de tradução json estruturado assim.
{
"STRING_ID_1": {
"en": "Some englisch text.",
"de": "Some german text."
},
"STRING_ID_2": {
"en": "Some other englisch text.",
"de": "Some other german text."
},
...
}
Usando isso como entrada, preciso gerar dois arquivos: "en.json" e "de.json". Eles devem ter o seguinte formato.
{
"STRING_ID_1": "Some englisch text.",
...
}
e
{
"STRING_ID_1": "Some german text.",
...
}
Parece-me que essa jq
é a ferramenta que você deseja usar aqui. Alguém pode me fornecer o comando correto?
Responder1
$ cat file.json
{
"STRING_ID_1": {
"en": "Some englisch text.",
"de": "Some german text."
},
"STRING_ID_2": {
"en": "Some other englisch text.",
"de": "Some other german text."
}
}
$ jq 'with_entries(.value = .value.en)' file.json
{
"STRING_ID_1": "Some englisch text.",
"STRING_ID_2": "Some other englisch text."
}
$ jq 'with_entries(.value = .value.de)' file.json
{
"STRING_ID_1": "Some german text.",
"STRING_ID_2": "Some other german text."
}
Redirecione para algum outro nome de arquivo para salvar a saída.
A transformação reescreve o valor de cada chave de nível superior para ser o valor da string em inglês (ou alemão) em vez de um objeto de pares de valores-chave.