Skip to content

Latest commit

 

History

History
103 lines (89 loc) · 2.47 KB

adding json objects from one file to another under single array using jq.md

File metadata and controls

103 lines (89 loc) · 2.47 KB

(jump to the answer)

I am new here so sorry if I do any mistakes while asking the question.

I have a json file that keeps updating every minute(File_1.json) with json objects. All i want to do is copy these objects to another file under a single array using the jq command.

Samples of files File_1.json:

        {
          "Id":"1",
          "Name":"Kiran",
          "Age":"12"
        }
        {
          "Id":"2",
          "Name":"Dileep",
          "Age":"22"
        }

Expected Output

     [ 
       {
          "Id":"1",
          "Name":"Kiran",
          "Age":"12"
        }
        {
          "Id":"2",
          "Name":"Dileep",
          "Age":"22"
        }
       ]

I have tried using -s(slurp) but since the code will be running once for every minute its creating multiple arrays.

A:

As of v1.76 jtc is capable of processing a stream of JSONs given as a file argument to either of options -i / -u. Given that capability the query is super easy:

let's start with an empty array in the output.json file:

bash $ <output.json jtc
[]
bash $ 

To add then to the file is trivial:

bash $ jtc -mi file_1.json -f output.json 
bash $ 
bash $ <output.json jtc
[
   {
      "Age": "12",
      "Id": "1",
      "Name": "Kiran"
   },
   {
      "Age": "22",
      "Id": "2",
      "Name": "Dileep"
   }
]
bash $ 
  • -m ensures merging of the input array (stream) of JSONs in file_1.json to the input file (output.json)
  • -f forces / redirects the output into the input file output.json instead of

If the same command executed once more - observe the extended array:

bash $ jtc -mi file_1.json -f output.json 
bash $ 
bash $ <output.json jtc
[
   {
      "Age": "12",
      "Id": "1",
      "Name": "Kiran"
   },
   {
      "Age": "22",
      "Id": "2",
      "Name": "Dileep"
   },
   {
      "Age": "12",
      "Id": "1",
      "Name": "Kiran"
   },
   {
      "Age": "22",
      "Id": "2",
      "Name": "Dileep"
   }
]
bash $