0

Hello, I have an englishStrings.json file and when I make new edits/ additions to it, I need to copy the new changes to all the other language string.json files.

Before making any changes to englishStrings, the englishStrings and otherLanguageStrings have the same keys and same number of lines but values differ.

I am trying to do something like this:

git diff -U0 —-word-diff-regex=. —-word-diff=porcelain englishStrings.json > changes.diff

patch frenchStrings.json < changes.diff

But it does not work for edits. The reason I want to apply character differences for when I insert a comma at the end of the key, value pair of an englishString and need to transfer it to another language strings file.

Examples: EnglishString.json

{
  "a": "one",
  "b": "two",
  "test": "test",
  "c": "three",
  "edit": "edit"
}

FrenchString.json

{
“a”: “un”,
“b”: “deux”,
“c”: “trois”
}
git diff -U0 —-word-diff-regex=. —-word-diff=porcelain EnglishString.json > changes.diff
@@ -4 +4,3 @@
   "
+test": "test",
~
+  "
 c": "three"
+,
~
+  "edit": "edit"
~

Expecting after patch:

{
  "a": "un",
  "b": "deux",
  "test": "test",
  "c": "trois",
  "edit": "edit"
}

But patch is currently telling me that the patch file is malformed. Patch probably wont be able to help since line length can differ between English and French Strings but I would be open to recommendations to change up my solution

1
  • @aviro I added the examples per your recommendation Sep 3 at 15:44

1 Answer 1

0

Rather than looking at the issue as a text patching problem, we may see it as a JSON structure mering task, overwriting the keys+values of one JSON data structure with those of another.

Assuming that the typographical (fancy) quotes in the French file are actually ordinary double quotes, you can use the JSON processor jq to merge the two data structures like this:

$ jq -s 'add' EnglishString.json FrenchString.json
{
  "a": "un",
  "b": "deux",
  "test": "test",
  "c": "trois",
  "edit": "edit"
}

This reads the "dictionary" of each file into an array element. The add instruction in the jq expression adds the two elements together, overwriting the keys+values read from the first file with the matching keys+values from the second.

In the example, the keys a, b, and c occur in both files, so the values from the last file are used.

6
  • Thanks @Kusalananda, My actual json files also have nested keys in them. I apologise for not reflecting that in the original post. How can we use your solution to merge json files that have embedded keys? Sep 3 at 16:18
  • @FreezerFridge I assume you will update your question to include a representable example.
    – Kusalananda
    Sep 3 at 16:19
  • @ Kusalananda so for some reason your solution works for minimal files and examples that I try to come up with but for the real large files, its not working as expected. Based on your new problem formulation, I came up with this jq -s '.[0] * .[1]' EnglishString.json FrenchString.json > merged.json && mv merged.json FrenchString.json however some of the keys are being re-ordered. Would you please help with that? Sep 3 at 17:26
  • @FreezerFridge The ordering of keys in JSON objects shouldn't be relevant to the application reading the data. The keys of a JSON object are like keys in a hash table; they have no inherent ordering. If you require an ordered data structure, you should use an array.
    – Kusalananda
    Sep 3 at 17:32
  • Yeah its not relevant but i would like to make the diff as small as possible when making a commit Sep 3 at 17:34

You must log in to answer this question.

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