Using jq with bash to run command for each object in array
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
add a comment |
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
add a comment |
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
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
bash jq
asked Apr 3 '17 at 19:13
danielrvtdanielrvt
3,49494688
3,49494688
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
add a comment |
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)
add a comment |
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.
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Apr 3 '17 at 19:25
chepnerchepner
246k32233324
246k32233324
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Apr 3 '17 at 19:46
kojirokojiro
52.8k1386137
52.8k1386137
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 13 '18 at 20:09
MarcinMarcin
35.1k1188171
35.1k1188171
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 13 '18 at 4:04
Haochen XieHaochen Xie
16018
16018
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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