jq - add objects from file into json array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I want to add an array with elements and value into an existing json file using jq.



I already have a file (input.json) with




"id": 9,
"version": 0,
"lastUpdTs": 1532371267968,
"name": "Training"



I want to add this into another groups array into this json (orig.json)



[

"name": "JAYS",
"sourceConnection":
"name": "ORACLE_connection",
"connectionType": "JDBC",
"commProtocol": "JDBC"
,
"checked": true,
"newlyAdded": false,
"id": null,
"groups": ,
"displayName": "SCOTT",
"defaultLevel": "MANAGED"

]


The end result should look like



[

"name": "JAYS",
"sourceConnection":
"name": "ORACLE_connection",
"connectionType": "JDBC",
"commProtocol": "JDBC"
,
"checked": true,
"newlyAdded": false,
"id": null,
"groups": [

"id": 9,
"version": 0,
"lastUpdTs": 1532371267968,
"name": "Training"

],
"displayName": "SCOTT",
"defaultLevel": "MANAGED"

]


I know how to add elements into an array, but not sure how to pass in from a file.



jq '..groups += ["INPUT": "HERE"]' ./orig.json









share|improve this question

























    up vote
    2
    down vote

    favorite












    I want to add an array with elements and value into an existing json file using jq.



    I already have a file (input.json) with




    "id": 9,
    "version": 0,
    "lastUpdTs": 1532371267968,
    "name": "Training"



    I want to add this into another groups array into this json (orig.json)



    [

    "name": "JAYS",
    "sourceConnection":
    "name": "ORACLE_connection",
    "connectionType": "JDBC",
    "commProtocol": "JDBC"
    ,
    "checked": true,
    "newlyAdded": false,
    "id": null,
    "groups": ,
    "displayName": "SCOTT",
    "defaultLevel": "MANAGED"

    ]


    The end result should look like



    [

    "name": "JAYS",
    "sourceConnection":
    "name": "ORACLE_connection",
    "connectionType": "JDBC",
    "commProtocol": "JDBC"
    ,
    "checked": true,
    "newlyAdded": false,
    "id": null,
    "groups": [

    "id": 9,
    "version": 0,
    "lastUpdTs": 1532371267968,
    "name": "Training"

    ],
    "displayName": "SCOTT",
    "defaultLevel": "MANAGED"

    ]


    I know how to add elements into an array, but not sure how to pass in from a file.



    jq '..groups += ["INPUT": "HERE"]' ./orig.json









    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to add an array with elements and value into an existing json file using jq.



      I already have a file (input.json) with




      "id": 9,
      "version": 0,
      "lastUpdTs": 1532371267968,
      "name": "Training"



      I want to add this into another groups array into this json (orig.json)



      [

      "name": "JAYS",
      "sourceConnection":
      "name": "ORACLE_connection",
      "connectionType": "JDBC",
      "commProtocol": "JDBC"
      ,
      "checked": true,
      "newlyAdded": false,
      "id": null,
      "groups": ,
      "displayName": "SCOTT",
      "defaultLevel": "MANAGED"

      ]


      The end result should look like



      [

      "name": "JAYS",
      "sourceConnection":
      "name": "ORACLE_connection",
      "connectionType": "JDBC",
      "commProtocol": "JDBC"
      ,
      "checked": true,
      "newlyAdded": false,
      "id": null,
      "groups": [

      "id": 9,
      "version": 0,
      "lastUpdTs": 1532371267968,
      "name": "Training"

      ],
      "displayName": "SCOTT",
      "defaultLevel": "MANAGED"

      ]


      I know how to add elements into an array, but not sure how to pass in from a file.



      jq '..groups += ["INPUT": "HERE"]' ./orig.json









      share|improve this question













      I want to add an array with elements and value into an existing json file using jq.



      I already have a file (input.json) with




      "id": 9,
      "version": 0,
      "lastUpdTs": 1532371267968,
      "name": "Training"



      I want to add this into another groups array into this json (orig.json)



      [

      "name": "JAYS",
      "sourceConnection":
      "name": "ORACLE_connection",
      "connectionType": "JDBC",
      "commProtocol": "JDBC"
      ,
      "checked": true,
      "newlyAdded": false,
      "id": null,
      "groups": ,
      "displayName": "SCOTT",
      "defaultLevel": "MANAGED"

      ]


      The end result should look like



      [

      "name": "JAYS",
      "sourceConnection":
      "name": "ORACLE_connection",
      "connectionType": "JDBC",
      "commProtocol": "JDBC"
      ,
      "checked": true,
      "newlyAdded": false,
      "id": null,
      "groups": [

      "id": 9,
      "version": 0,
      "lastUpdTs": 1532371267968,
      "name": "Training"

      ],
      "displayName": "SCOTT",
      "defaultLevel": "MANAGED"

      ]


      I know how to add elements into an array, but not sure how to pass in from a file.



      jq '..groups += ["INPUT": "HERE"]' ./orig.json






      array json jq






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 7 at 7:38









      CLO

      152




      152




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON



          jq --argjson groupInfo "$(<input.json)" '..groups += [$groupInfo]' orig.json


          The part "$(<input.json)" is shell re-direction construct to output the contents of the file given and with the argument to --argjson it is stored in the variable groupInfo. Now you add it to the groups array in the actual filter part.



          Putting it in another way, the above solution is equivalent of doing this



          jq --argjson groupInfo '"id": 9,"version": 0,"lastUpdTs": 1532371267968,"name": "Training" ' 
          '..groups += [$groupInfo]' orig.json





          share|improve this answer


















          • 1




            Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
            – Michael Homer
            Aug 7 at 8:26

















          up vote
          3
          down vote













          This is the exact case that the input function is for:




          input and inputs [...] read from the same sources (e.g., stdin, files named on the command-line) as jq itself. These two builtins, and jq’s own reading actions, can be interleaved with each other.




          That is, jq reads an object/value in from the file and executes the pipeline on it, and anywhere input appears the next input is read in and is used as the result of the function.



          That means you can do:



          jq '..groups += [input]' orig.json input.json


          with exactly the command you've written already, plus input as the value. The input expression will evaluate to the (first) object read from the next file in the argument list, in this case the entire contents of input.json.



          If you have multiple items to insert you can use inputs instead with the same meaning. It will apply across a single or multiple files from the command line equally, and [inputs] represents all the file bodies as an array.



          It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460985%2fjq-add-objects-from-file-into-json-array%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON



            jq --argjson groupInfo "$(<input.json)" '..groups += [$groupInfo]' orig.json


            The part "$(<input.json)" is shell re-direction construct to output the contents of the file given and with the argument to --argjson it is stored in the variable groupInfo. Now you add it to the groups array in the actual filter part.



            Putting it in another way, the above solution is equivalent of doing this



            jq --argjson groupInfo '"id": 9,"version": 0,"lastUpdTs": 1532371267968,"name": "Training" ' 
            '..groups += [$groupInfo]' orig.json





            share|improve this answer


















            • 1




              Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
              – Michael Homer
              Aug 7 at 8:26














            up vote
            2
            down vote



            accepted










            jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON



            jq --argjson groupInfo "$(<input.json)" '..groups += [$groupInfo]' orig.json


            The part "$(<input.json)" is shell re-direction construct to output the contents of the file given and with the argument to --argjson it is stored in the variable groupInfo. Now you add it to the groups array in the actual filter part.



            Putting it in another way, the above solution is equivalent of doing this



            jq --argjson groupInfo '"id": 9,"version": 0,"lastUpdTs": 1532371267968,"name": "Training" ' 
            '..groups += [$groupInfo]' orig.json





            share|improve this answer


















            • 1




              Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
              – Michael Homer
              Aug 7 at 8:26












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON



            jq --argjson groupInfo "$(<input.json)" '..groups += [$groupInfo]' orig.json


            The part "$(<input.json)" is shell re-direction construct to output the contents of the file given and with the argument to --argjson it is stored in the variable groupInfo. Now you add it to the groups array in the actual filter part.



            Putting it in another way, the above solution is equivalent of doing this



            jq --argjson groupInfo '"id": 9,"version": 0,"lastUpdTs": 1532371267968,"name": "Training" ' 
            '..groups += [$groupInfo]' orig.json





            share|improve this answer














            jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON



            jq --argjson groupInfo "$(<input.json)" '..groups += [$groupInfo]' orig.json


            The part "$(<input.json)" is shell re-direction construct to output the contents of the file given and with the argument to --argjson it is stored in the variable groupInfo. Now you add it to the groups array in the actual filter part.



            Putting it in another way, the above solution is equivalent of doing this



            jq --argjson groupInfo '"id": 9,"version": 0,"lastUpdTs": 1532371267968,"name": "Training" ' 
            '..groups += [$groupInfo]' orig.json






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 7 at 8:02

























            answered Aug 7 at 7:43









            Inian

            2,855822




            2,855822







            • 1




              Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
              – Michael Homer
              Aug 7 at 8:26












            • 1




              Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
              – Michael Homer
              Aug 7 at 8:26







            1




            1




            Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
            – Michael Homer
            Aug 7 at 8:26




            Noting that $(<file) is a ksh/bash/zsh extension and not otherwise portable, however. You could use cat instead.
            – Michael Homer
            Aug 7 at 8:26












            up vote
            3
            down vote













            This is the exact case that the input function is for:




            input and inputs [...] read from the same sources (e.g., stdin, files named on the command-line) as jq itself. These two builtins, and jq’s own reading actions, can be interleaved with each other.




            That is, jq reads an object/value in from the file and executes the pipeline on it, and anywhere input appears the next input is read in and is used as the result of the function.



            That means you can do:



            jq '..groups += [input]' orig.json input.json


            with exactly the command you've written already, plus input as the value. The input expression will evaluate to the (first) object read from the next file in the argument list, in this case the entire contents of input.json.



            If you have multiple items to insert you can use inputs instead with the same meaning. It will apply across a single or multiple files from the command line equally, and [inputs] represents all the file bodies as an array.



            It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle.






            share|improve this answer
























              up vote
              3
              down vote













              This is the exact case that the input function is for:




              input and inputs [...] read from the same sources (e.g., stdin, files named on the command-line) as jq itself. These two builtins, and jq’s own reading actions, can be interleaved with each other.




              That is, jq reads an object/value in from the file and executes the pipeline on it, and anywhere input appears the next input is read in and is used as the result of the function.



              That means you can do:



              jq '..groups += [input]' orig.json input.json


              with exactly the command you've written already, plus input as the value. The input expression will evaluate to the (first) object read from the next file in the argument list, in this case the entire contents of input.json.



              If you have multiple items to insert you can use inputs instead with the same meaning. It will apply across a single or multiple files from the command line equally, and [inputs] represents all the file bodies as an array.



              It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle.






              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                This is the exact case that the input function is for:




                input and inputs [...] read from the same sources (e.g., stdin, files named on the command-line) as jq itself. These two builtins, and jq’s own reading actions, can be interleaved with each other.




                That is, jq reads an object/value in from the file and executes the pipeline on it, and anywhere input appears the next input is read in and is used as the result of the function.



                That means you can do:



                jq '..groups += [input]' orig.json input.json


                with exactly the command you've written already, plus input as the value. The input expression will evaluate to the (first) object read from the next file in the argument list, in this case the entire contents of input.json.



                If you have multiple items to insert you can use inputs instead with the same meaning. It will apply across a single or multiple files from the command line equally, and [inputs] represents all the file bodies as an array.



                It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle.






                share|improve this answer












                This is the exact case that the input function is for:




                input and inputs [...] read from the same sources (e.g., stdin, files named on the command-line) as jq itself. These two builtins, and jq’s own reading actions, can be interleaved with each other.




                That is, jq reads an object/value in from the file and executes the pipeline on it, and anywhere input appears the next input is read in and is used as the result of the function.



                That means you can do:



                jq '..groups += [input]' orig.json input.json


                with exactly the command you've written already, plus input as the value. The input expression will evaluate to the (first) object read from the next file in the argument list, in this case the entire contents of input.json.



                If you have multiple items to insert you can use inputs instead with the same meaning. It will apply across a single or multiple files from the command line equally, and [inputs] represents all the file bodies as an array.



                It's also possible to interleave things to process multiple orig files, each with one companion file inserted, but separating the outputs would be a hassle.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 7 at 7:54









                Michael Homer

                42.7k6108148




                42.7k6108148



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460985%2fjq-add-objects-from-file-into-json-array%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?