if else in jq is not giving expected output
I need to display users if their realm is internal.
Input:
[
{
"name": "A_A",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "B_B",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "C_C",
"uri": "https:test/test",
"realm": "external"
}
]
Tried with:
jq 'if ..realm == "internal" then ..name else empty end'
But the problem is that it is listing all the users.
Expected output:
A_A , B_B
jq
add a comment |
I need to display users if their realm is internal.
Input:
[
{
"name": "A_A",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "B_B",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "C_C",
"uri": "https:test/test",
"realm": "external"
}
]
Tried with:
jq 'if ..realm == "internal" then ..name else empty end'
But the problem is that it is listing all the users.
Expected output:
A_A , B_B
jq
I would look at the Elastic search syntax, if you are talking to one. I am not exactly on expert on that, I often do my queries though trial and error, so not the best person to write an answer.
– Rui F Ribeiro
13 hours ago
. | (if .realm == "internal" then .name else empty end)
. You have to put your conditional after you iterated into the individual item you want to test.
– Charles Duffy
10 hours ago
add a comment |
I need to display users if their realm is internal.
Input:
[
{
"name": "A_A",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "B_B",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "C_C",
"uri": "https:test/test",
"realm": "external"
}
]
Tried with:
jq 'if ..realm == "internal" then ..name else empty end'
But the problem is that it is listing all the users.
Expected output:
A_A , B_B
jq
I need to display users if their realm is internal.
Input:
[
{
"name": "A_A",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "B_B",
"uri": "https:test/test",
"realm": "internal"
},
{
"name": "C_C",
"uri": "https:test/test",
"realm": "external"
}
]
Tried with:
jq 'if ..realm == "internal" then ..name else empty end'
But the problem is that it is listing all the users.
Expected output:
A_A , B_B
jq
jq
edited 13 hours ago
Jeff Schaller
41.7k1156133
41.7k1156133
asked 14 hours ago
Sugatur Deekshith S NSugatur Deekshith S N
376
376
I would look at the Elastic search syntax, if you are talking to one. I am not exactly on expert on that, I often do my queries though trial and error, so not the best person to write an answer.
– Rui F Ribeiro
13 hours ago
. | (if .realm == "internal" then .name else empty end)
. You have to put your conditional after you iterated into the individual item you want to test.
– Charles Duffy
10 hours ago
add a comment |
I would look at the Elastic search syntax, if you are talking to one. I am not exactly on expert on that, I often do my queries though trial and error, so not the best person to write an answer.
– Rui F Ribeiro
13 hours ago
. | (if .realm == "internal" then .name else empty end)
. You have to put your conditional after you iterated into the individual item you want to test.
– Charles Duffy
10 hours ago
I would look at the Elastic search syntax, if you are talking to one. I am not exactly on expert on that, I often do my queries though trial and error, so not the best person to write an answer.
– Rui F Ribeiro
13 hours ago
I would look at the Elastic search syntax, if you are talking to one. I am not exactly on expert on that, I often do my queries though trial and error, so not the best person to write an answer.
– Rui F Ribeiro
13 hours ago
. | (if .realm == "internal" then .name else empty end)
. You have to put your conditional after you iterated into the individual item you want to test.– Charles Duffy
10 hours ago
. | (if .realm == "internal" then .name else empty end)
. You have to put your conditional after you iterated into the individual item you want to test.– Charles Duffy
10 hours ago
add a comment |
1 Answer
1
active
oldest
votes
You can use the jq function select
:
<file jq -r '. | select(.realm == "internal") | .name'
The first .
gets the array elements. The select()
applies to individual element and filter the ones have the correct realm
. The last part prints the name
field.
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
1
If you'd rather have an array, usemap(select(.realm == "internal") | .name)
instead. That would let you| join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output
– Aaron
12 hours ago
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f501577%2fif-else-in-jq-is-not-giving-expected-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the jq function select
:
<file jq -r '. | select(.realm == "internal") | .name'
The first .
gets the array elements. The select()
applies to individual element and filter the ones have the correct realm
. The last part prints the name
field.
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
1
If you'd rather have an array, usemap(select(.realm == "internal") | .name)
instead. That would let you| join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output
– Aaron
12 hours ago
add a comment |
You can use the jq function select
:
<file jq -r '. | select(.realm == "internal") | .name'
The first .
gets the array elements. The select()
applies to individual element and filter the ones have the correct realm
. The last part prints the name
field.
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
1
If you'd rather have an array, usemap(select(.realm == "internal") | .name)
instead. That would let you| join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output
– Aaron
12 hours ago
add a comment |
You can use the jq function select
:
<file jq -r '. | select(.realm == "internal") | .name'
The first .
gets the array elements. The select()
applies to individual element and filter the ones have the correct realm
. The last part prints the name
field.
You can use the jq function select
:
<file jq -r '. | select(.realm == "internal") | .name'
The first .
gets the array elements. The select()
applies to individual element and filter the ones have the correct realm
. The last part prints the name
field.
answered 13 hours ago
olivoliv
1,766412
1,766412
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
1
If you'd rather have an array, usemap(select(.realm == "internal") | .name)
instead. That would let you| join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output
– Aaron
12 hours ago
add a comment |
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
1
If you'd rather have an array, usemap(select(.realm == "internal") | .name)
instead. That would let you| join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output
– Aaron
12 hours ago
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
cool , this works for me
– Sugatur Deekshith S N
13 hours ago
1
1
If you'd rather have an array, use
map(select(.realm == "internal") | .name)
instead. That would let you | join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output– Aaron
12 hours ago
If you'd rather have an array, use
map(select(.realm == "internal") | .name)
instead. That would let you | join(delim)
with a specific delimiter if you need to : here's a sample with your exact expected output– Aaron
12 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f501577%2fif-else-in-jq-is-not-giving-expected-output%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
I would look at the Elastic search syntax, if you are talking to one. I am not exactly on expert on that, I often do my queries though trial and error, so not the best person to write an answer.
– Rui F Ribeiro
13 hours ago
. | (if .realm == "internal" then .name else empty end)
. You have to put your conditional after you iterated into the individual item you want to test.– Charles Duffy
10 hours ago