2

I have a json translation file that is structured like this.

{
  "STRING_ID_1": {
    "en": "Some englisch text.",
    "de": "Some german text."
  },
  "STRING_ID_2": {
    "en": "Some other englisch text.",
    "de": "Some other german text."
  },
  ...
}

Using this as input, I need to generate two files: "en.json" and "de.json". They should have the following format.

{
  "STRING_ID_1": "Some englisch text.",
  ...
}

and

{
  "STRING_ID_1": "Some german text.",
  ...
}

It seems to me that jq is the tool you want to use here. Can anyone provide me with the correct command?

1 Answer 1

4
$ 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."
}

Redirect to some other filename to save the output.

The transformation rewrites the value of each top-level key to be the value of the English (or German) string instead of an object of key-value pairs.

2
  • Thank you. I have an additional question: As it turns out, there are some ids that are only present in English. Currently they are stored with a null value. How do I simply omit them? Aug 12, 2020 at 12:43
  • 1
    @PhilippSiegmantel jq 'with_entries(.value = .value.en | select(.value != null))' file.json If I understand you correctly...
    – Kusalananda
    Aug 12, 2020 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .