Using jq with bash to run command for each object in array










10















How can I run a bash command for every json object in a json array using jq? So far I have this:



cat credentials.json | jq -r '. | .user, .date, .email' | mycommand -u user -d date -e email


This doesn't seem to work. How can I take the parameters out of the json array into my command?



My json file looks something like this:



[
"user": "danielrvt",
"date": "11/10/1988",
"email": "myemail@domain.com",
...
]









share|improve this question


























    10















    How can I run a bash command for every json object in a json array using jq? So far I have this:



    cat credentials.json | jq -r '. | .user, .date, .email' | mycommand -u user -d date -e email


    This doesn't seem to work. How can I take the parameters out of the json array into my command?



    My json file looks something like this:



    [
    "user": "danielrvt",
    "date": "11/10/1988",
    "email": "myemail@domain.com",
    ...
    ]









    share|improve this question
























      10












      10








      10


      1






      How can I run a bash command for every json object in a json array using jq? So far I have this:



      cat credentials.json | jq -r '. | .user, .date, .email' | mycommand -u user -d date -e email


      This doesn't seem to work. How can I take the parameters out of the json array into my command?



      My json file looks something like this:



      [
      "user": "danielrvt",
      "date": "11/10/1988",
      "email": "myemail@domain.com",
      ...
      ]









      share|improve this question














      How can I run a bash command for every json object in a json array using jq? So far I have this:



      cat credentials.json | jq -r '. | .user, .date, .email' | mycommand -u user -d date -e email


      This doesn't seem to work. How can I take the parameters out of the json array into my command?



      My json file looks something like this:



      [
      "user": "danielrvt",
      "date": "11/10/1988",
      "email": "myemail@domain.com",
      ...
      ]






      bash jq






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 3 '17 at 19:13









      danielrvtdanielrvt

      3,49494688




      3,49494688






















          4 Answers
          4






          active

          oldest

          votes


















          14














          Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.



          jq -r '.|[.user, .date, .email] | @tsv' |
          while IFS=$'t' read -r user date email; do
          mycommand -u "$user" -d "$date" -e "$email"
          done


          jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.






          share|improve this answer






























            4














            You could have jq output the commands to execute, something like



            . | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"


            Then execute it. Something like



            bash <(jq -r '. | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"' foo)





            share|improve this answer






























              4














              With xargs:



              curl localhost:8082/connectors | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


              Or equivalently, to show the output of that first curl:



              echo '["quickstart-file-sink4","quickstart-file-source","quickstart-file-sink","quickstart-file-sink2","quickstart-file-sink3","quickstart-file-source2"]' | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


              jq . strips off one level of containment, so that a list becomes output as one line per item.



              xargs -L1 processes one line at a time



              xargs -I'' specifies that the string be replaced with the input line when invoking the following command.



              xargs is essentially a map operator for the shell.






              share|improve this answer






























                1














                I came across the same problem recently where xargs doesn't help that much due to the relatively complicated set of arguments I wanted to pass around. Thus I implemented an sh filter (and its friends) to jq. I haven't yet had enough time to write documentation and tests for it so not creating a PR for it to become a part of the official codebase yet. So now it's only for the ones who are willing to compile this version themselves:



                https://github.com/haochenx/jq/tree/sh-support






                share|improve this answer






















                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  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',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43192556%2fusing-jq-with-bash-to-run-command-for-each-object-in-array%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  14














                  Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.



                  jq -r '.|[.user, .date, .email] | @tsv' |
                  while IFS=$'t' read -r user date email; do
                  mycommand -u "$user" -d "$date" -e "$email"
                  done


                  jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.






                  share|improve this answer



























                    14














                    Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.



                    jq -r '.|[.user, .date, .email] | @tsv' |
                    while IFS=$'t' read -r user date email; do
                    mycommand -u "$user" -d "$date" -e "$email"
                    done


                    jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.






                    share|improve this answer

























                      14












                      14








                      14







                      Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.



                      jq -r '.|[.user, .date, .email] | @tsv' |
                      while IFS=$'t' read -r user date email; do
                      mycommand -u "$user" -d "$date" -e "$email"
                      done


                      jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.






                      share|improve this answer













                      Your best bet is probably to output each record in something like TSV format, then read that from a shell loop.



                      jq -r '.|[.user, .date, .email] | @tsv' |
                      while IFS=$'t' read -r user date email; do
                      mycommand -u "$user" -d "$date" -e "$email"
                      done


                      jq itself doesn't have anything like a system call to run an external command from within a filter, although it seems that they are working on it.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 3 '17 at 19:25









                      chepnerchepner

                      246k32233324




                      246k32233324























                          4














                          You could have jq output the commands to execute, something like



                          . | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"


                          Then execute it. Something like



                          bash <(jq -r '. | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"' foo)





                          share|improve this answer



























                            4














                            You could have jq output the commands to execute, something like



                            . | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"


                            Then execute it. Something like



                            bash <(jq -r '. | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"' foo)





                            share|improve this answer

























                              4












                              4








                              4







                              You could have jq output the commands to execute, something like



                              . | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"


                              Then execute it. Something like



                              bash <(jq -r '. | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"' foo)





                              share|improve this answer













                              You could have jq output the commands to execute, something like



                              . | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"


                              Then execute it. Something like



                              bash <(jq -r '. | "mycommand (.user|@sh) (.date|@sh) (.email|@sh)"' foo)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 3 '17 at 19:46









                              kojirokojiro

                              52.8k1386137




                              52.8k1386137





















                                  4














                                  With xargs:



                                  curl localhost:8082/connectors | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                  Or equivalently, to show the output of that first curl:



                                  echo '["quickstart-file-sink4","quickstart-file-source","quickstart-file-sink","quickstart-file-sink2","quickstart-file-sink3","quickstart-file-source2"]' | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                  jq . strips off one level of containment, so that a list becomes output as one line per item.



                                  xargs -L1 processes one line at a time



                                  xargs -I'' specifies that the string be replaced with the input line when invoking the following command.



                                  xargs is essentially a map operator for the shell.






                                  share|improve this answer



























                                    4














                                    With xargs:



                                    curl localhost:8082/connectors | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                    Or equivalently, to show the output of that first curl:



                                    echo '["quickstart-file-sink4","quickstart-file-source","quickstart-file-sink","quickstart-file-sink2","quickstart-file-sink3","quickstart-file-source2"]' | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                    jq . strips off one level of containment, so that a list becomes output as one line per item.



                                    xargs -L1 processes one line at a time



                                    xargs -I'' specifies that the string be replaced with the input line when invoking the following command.



                                    xargs is essentially a map operator for the shell.






                                    share|improve this answer

























                                      4












                                      4








                                      4







                                      With xargs:



                                      curl localhost:8082/connectors | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                      Or equivalently, to show the output of that first curl:



                                      echo '["quickstart-file-sink4","quickstart-file-source","quickstart-file-sink","quickstart-file-sink2","quickstart-file-sink3","quickstart-file-source2"]' | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                      jq . strips off one level of containment, so that a list becomes output as one line per item.



                                      xargs -L1 processes one line at a time



                                      xargs -I'' specifies that the string be replaced with the input line when invoking the following command.



                                      xargs is essentially a map operator for the shell.






                                      share|improve this answer













                                      With xargs:



                                      curl localhost:8082/connectors | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                      Or equivalently, to show the output of that first curl:



                                      echo '["quickstart-file-sink4","quickstart-file-source","quickstart-file-sink","quickstart-file-sink2","quickstart-file-sink3","quickstart-file-source2"]' | jq . | xargs -L1 -I'' curl -XDELETE 'localhost:8082/connectors/' 


                                      jq . strips off one level of containment, so that a list becomes output as one line per item.



                                      xargs -L1 processes one line at a time



                                      xargs -I'' specifies that the string be replaced with the input line when invoking the following command.



                                      xargs is essentially a map operator for the shell.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jun 13 '18 at 20:09









                                      MarcinMarcin

                                      35.1k1188171




                                      35.1k1188171





















                                          1














                                          I came across the same problem recently where xargs doesn't help that much due to the relatively complicated set of arguments I wanted to pass around. Thus I implemented an sh filter (and its friends) to jq. I haven't yet had enough time to write documentation and tests for it so not creating a PR for it to become a part of the official codebase yet. So now it's only for the ones who are willing to compile this version themselves:



                                          https://github.com/haochenx/jq/tree/sh-support






                                          share|improve this answer



























                                            1














                                            I came across the same problem recently where xargs doesn't help that much due to the relatively complicated set of arguments I wanted to pass around. Thus I implemented an sh filter (and its friends) to jq. I haven't yet had enough time to write documentation and tests for it so not creating a PR for it to become a part of the official codebase yet. So now it's only for the ones who are willing to compile this version themselves:



                                            https://github.com/haochenx/jq/tree/sh-support






                                            share|improve this answer

























                                              1












                                              1








                                              1







                                              I came across the same problem recently where xargs doesn't help that much due to the relatively complicated set of arguments I wanted to pass around. Thus I implemented an sh filter (and its friends) to jq. I haven't yet had enough time to write documentation and tests for it so not creating a PR for it to become a part of the official codebase yet. So now it's only for the ones who are willing to compile this version themselves:



                                              https://github.com/haochenx/jq/tree/sh-support






                                              share|improve this answer













                                              I came across the same problem recently where xargs doesn't help that much due to the relatively complicated set of arguments I wanted to pass around. Thus I implemented an sh filter (and its friends) to jq. I haven't yet had enough time to write documentation and tests for it so not creating a PR for it to become a part of the official codebase yet. So now it's only for the ones who are willing to compile this version themselves:



                                              https://github.com/haochenx/jq/tree/sh-support







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 13 '18 at 4:04









                                              Haochen XieHaochen Xie

                                              16018




                                              16018



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43192556%2fusing-jq-with-bash-to-run-command-for-each-object-in-array%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  這個網誌中的熱門文章

                                                  Barbados

                                                  How to read a connectionString WITH PROVIDER in .NET Core?

                                                  Node.js Script on GitHub Pages or Amazon S3