3

I have a json file named xyz.json as follows

[
    {
        "annotations": [ "a" , "b" , "c" ],
        "class": "image",
        "filename": "vc00_02201.png"
    },
    {
        "annotations": [],
        "class": "image",
        "filename": "vc00_02202.png"
    }
    {
        "annotations": [],
        "class": "image",
        "filename": "vc00_02203.png"
    },
    {
        "annotations": [],
        "class": "image",
        "filename": "vc00_02204.png"
    }
]

I need to copy the object "annotations" from "filename": "vc00_02201.png" to "annotations" of "filename": "vc00_02204.png" within the same xyz.json file using a shell script. Expected output:

[
    {
        "annotations": [ "a" , "b" , "c" ],
        "class": "image",
        "filename": "vc00_02201.png"
    },
    {
        "annotations": [],
        "class": "image",
        "filename": "vc00_02202.png"
    }
    {
        "annotations": [],
        "class": "image",
        "filename": "vc00_02203.png"
    },
    {
        "annotations": [ "a" , "b" , "c" ],
        "class": "image",
        "filename": "vc00_02204.png"
    }
]
2
  • Does it have to be shell script? This would certainly be easier in a scripting language with JSON support (all of the major ones, including at least Perl, Python, and Ruby). Also, if it has to be shell, which external utilities can you use? Can you use something like jq ?
    – derobert
    Jan 8, 2019 at 18:03
  • yes I can use jq and python script is also fine. Jan 8, 2019 at 18:15

2 Answers 2

3
$cat xyz.json | jq '.[3].annotations=.[0].annotations'
4
  • 2
    +1, You can do jq ... xyz.json as well instead of using cat.
    – Haxiel
    Jan 9, 2019 at 7:06
  • it says xyz.json : command not found Jan 9, 2019 at 12:53
  • don't copy the "$" sign before cat
    – Neo Mosaid
    Jan 9, 2019 at 16:20
  • It says the same with out copy of $ sign Jan 9, 2019 at 18:20
0

this is also achievable via jtc in one line:

bash $ cat xyz.json | jtc -w'<vc00_02204.png>[-1][annotations]' -eu jtc -w'<vc00_02201.png>[-1][annotations]' xyz.json \; 
[
   {
      "annotations": [
         "a",
         "b",
         "c"
      ],
      "class": "image",
      "filename": "vc00_02201.png"
   },
   {
      "annotations": [],
      "class": "image",
      "filename": "vc00_02202.png"
   },
   {
      "annotations": [],
      "class": "image",
      "filename": "vc00_02203.png"
   },
   {
      "annotations": [
         "a",
         "b",
         "c"
      ],
      "class": "image",
      "filename": "vc00_02204.png"
   }
]
bash $ 

Of course, it could have been done via referencing direct members of array, like:

bash $ cat xyz.json | jtc -w'[3][annotations]' -eu jtc -w'[0][annotations]' xyz.json \;

but then whole point of cross-referencing by filename would be moot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.