if else in jq is not giving expected output












3















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









share|improve this question

























  • 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


















3















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









share|improve this question

























  • 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
















3












3








3








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





















  • 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












1 Answer
1






active

oldest

votes


















10














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.






share|improve this answer
























  • cool , this works for me

    – Sugatur Deekshith S N
    13 hours ago






  • 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













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
});


}
});














draft saved

draft discarded


















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









10














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.






share|improve this answer
























  • cool , this works for me

    – Sugatur Deekshith S N
    13 hours ago






  • 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


















10














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.






share|improve this answer
























  • cool , this works for me

    – Sugatur Deekshith S N
    13 hours ago






  • 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
















10












10








10







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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, 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





















  • cool , this works for me

    – Sugatur Deekshith S N
    13 hours ago






  • 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



















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana