Extract json array element based on a subelement value

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











up vote
2
down vote

favorite
1












We have the following example file ( very long file , this is short example )



"request_status" : "FAILED"

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests" :
"cluster_name" : "sys41",
"id" : 333,
"request_status" : "COMPLETED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/334",
"Requests" :
"cluster_name" : "sys41",
"id" : 334,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/335",
"Requests" :
"cluster_name" : "sys41",
"id" : 335,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/336",
"Requests" :
"cluster_name" : "sys41",
"id" : 336,
"request_status" : "COMPLETED"




how to print the line after the line that matches "id" : $num

e.g. for



num=335 


how to get the line after



"id" : $num


Expected output



"request_status" : "FAILED"






share|improve this question


















  • 3




    This looks like a job for jq which is the tool specifically built for querying JSON objects, but I am not fluent enough to impart unto you the correct incantation.
    – DopeGhoti
    Mar 5 at 17:16







  • 1




    Could you please post a less broken JSON file? It's difficult to come up with a proper jq solution when the JSON is so severely mangled at the top.
    – Kusalananda
    Mar 5 at 17:40






  • 1




    If it's really json you're dealing with, then be careful using solutions that assume a fixed number of spaces around the colons (and all the other flexible parts of the json spec!)
    – Jeff Schaller
    Mar 5 at 17:44














up vote
2
down vote

favorite
1












We have the following example file ( very long file , this is short example )



"request_status" : "FAILED"

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests" :
"cluster_name" : "sys41",
"id" : 333,
"request_status" : "COMPLETED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/334",
"Requests" :
"cluster_name" : "sys41",
"id" : 334,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/335",
"Requests" :
"cluster_name" : "sys41",
"id" : 335,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/336",
"Requests" :
"cluster_name" : "sys41",
"id" : 336,
"request_status" : "COMPLETED"




how to print the line after the line that matches "id" : $num

e.g. for



num=335 


how to get the line after



"id" : $num


Expected output



"request_status" : "FAILED"






share|improve this question


















  • 3




    This looks like a job for jq which is the tool specifically built for querying JSON objects, but I am not fluent enough to impart unto you the correct incantation.
    – DopeGhoti
    Mar 5 at 17:16







  • 1




    Could you please post a less broken JSON file? It's difficult to come up with a proper jq solution when the JSON is so severely mangled at the top.
    – Kusalananda
    Mar 5 at 17:40






  • 1




    If it's really json you're dealing with, then be careful using solutions that assume a fixed number of spaces around the colons (and all the other flexible parts of the json spec!)
    – Jeff Schaller
    Mar 5 at 17:44












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





We have the following example file ( very long file , this is short example )



"request_status" : "FAILED"

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests" :
"cluster_name" : "sys41",
"id" : 333,
"request_status" : "COMPLETED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/334",
"Requests" :
"cluster_name" : "sys41",
"id" : 334,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/335",
"Requests" :
"cluster_name" : "sys41",
"id" : 335,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/336",
"Requests" :
"cluster_name" : "sys41",
"id" : 336,
"request_status" : "COMPLETED"




how to print the line after the line that matches "id" : $num

e.g. for



num=335 


how to get the line after



"id" : $num


Expected output



"request_status" : "FAILED"






share|improve this question














We have the following example file ( very long file , this is short example )



"request_status" : "FAILED"

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests" :
"cluster_name" : "sys41",
"id" : 333,
"request_status" : "COMPLETED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/334",
"Requests" :
"cluster_name" : "sys41",
"id" : 334,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/335",
"Requests" :
"cluster_name" : "sys41",
"id" : 335,
"request_status" : "FAILED"

,

"href" : "http://localhost:8080/api/v1/clusters/sys41/requests/336",
"Requests" :
"cluster_name" : "sys41",
"id" : 336,
"request_status" : "COMPLETED"




how to print the line after the line that matches "id" : $num

e.g. for



num=335 


how to get the line after



"id" : $num


Expected output



"request_status" : "FAILED"








share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 1:31









Drakonoved

674518




674518










asked Mar 5 at 17:09









yael

1,9501145




1,9501145







  • 3




    This looks like a job for jq which is the tool specifically built for querying JSON objects, but I am not fluent enough to impart unto you the correct incantation.
    – DopeGhoti
    Mar 5 at 17:16







  • 1




    Could you please post a less broken JSON file? It's difficult to come up with a proper jq solution when the JSON is so severely mangled at the top.
    – Kusalananda
    Mar 5 at 17:40






  • 1




    If it's really json you're dealing with, then be careful using solutions that assume a fixed number of spaces around the colons (and all the other flexible parts of the json spec!)
    – Jeff Schaller
    Mar 5 at 17:44












  • 3




    This looks like a job for jq which is the tool specifically built for querying JSON objects, but I am not fluent enough to impart unto you the correct incantation.
    – DopeGhoti
    Mar 5 at 17:16







  • 1




    Could you please post a less broken JSON file? It's difficult to come up with a proper jq solution when the JSON is so severely mangled at the top.
    – Kusalananda
    Mar 5 at 17:40






  • 1




    If it's really json you're dealing with, then be careful using solutions that assume a fixed number of spaces around the colons (and all the other flexible parts of the json spec!)
    – Jeff Schaller
    Mar 5 at 17:44







3




3




This looks like a job for jq which is the tool specifically built for querying JSON objects, but I am not fluent enough to impart unto you the correct incantation.
– DopeGhoti
Mar 5 at 17:16





This looks like a job for jq which is the tool specifically built for querying JSON objects, but I am not fluent enough to impart unto you the correct incantation.
– DopeGhoti
Mar 5 at 17:16





1




1




Could you please post a less broken JSON file? It's difficult to come up with a proper jq solution when the JSON is so severely mangled at the top.
– Kusalananda
Mar 5 at 17:40




Could you please post a less broken JSON file? It's difficult to come up with a proper jq solution when the JSON is so severely mangled at the top.
– Kusalananda
Mar 5 at 17:40




1




1




If it's really json you're dealing with, then be careful using solutions that assume a fixed number of spaces around the colons (and all the other flexible parts of the json spec!)
– Jeff Schaller
Mar 5 at 17:44




If it's really json you're dealing with, then be careful using solutions that assume a fixed number of spaces around the colons (and all the other flexible parts of the json spec!)
– Jeff Schaller
Mar 5 at 17:44










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










Given the shell variable num set equal to 335:



grep -A1 ""id" : $num," /path/to/file


I get a lot of mileage out of grep's handy -A, -B, and -C options, the mnemonics for which I use are "After", "Before", and "Context".



  • grep -A n 'pattern' file will show the n lines After a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines Before a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines both before and after (i. e. the Context of) a pattern match, in addition to the matching line itself.






share|improve this answer






















  • For less toothpicks: grep -A1 '"id" : '"$num," ...
    – Kusalananda
    Mar 5 at 18:50






  • 1




    Just as many toothpicks; they're just shorter (:
    – DopeGhoti
    Mar 5 at 18:55

















up vote
6
down vote













Assuming that the JSON file is one big array of objects such as



 
"href": "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests": "id": 333, "cluster_name": "sys41", "request_status": "COMPLETED"
,


(noting that newlines are not significant, and neither is the ordering of the keys in an object) then, the following jq command would get the request_status entry for the id corresponding to $num:



$ num=355
$ jq -r "..Requests | select(.id == $num).request_status" file.json
FAILED


It does this by picking out all the Requests entries from each object and filtering them on the id. For the ones that pass the filter, it extracts request_status.



The -r option to jq makes it output raw data instead of JSON (FAILED would have been double quoted without it).




Bonus material:



$ jq -r '..Requests | "(.id): (.request_status)"' file.json
333: COMPLETED
334: FAILED
335: FAILED
336: COMPLETED





share|improve this answer


















  • 2




    jq is the right tool for the job.
    – Zachary Brady
    Mar 5 at 18:36

















up vote
1
down vote













awk '/"id" : 335/getline;print'


or



var=""id" : 335"
awk "/$var/getline;print"


or



awk -v var=""id" : 335" '$0~vargetline;print'


or



var=""id" : 335"
awk -v var="$var" '$0~vargetline;print'


or any of the above, but without needing the "id" : " part of the string, in the form:



awk -v var="335" '$0~"id" : vargetline;print'





share|improve this answer






















  • May not work as intended. For example, will show a false match for "id" : 1355.
    – DopeGhoti
    Mar 5 at 17:24










  • Did you try that and have it fail?
    – user1404316
    Mar 5 at 17:27










  • Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
    – user1404316
    Mar 5 at 17:29










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%2f428323%2fextract-json-array-element-based-on-a-subelement-value%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Given the shell variable num set equal to 335:



grep -A1 ""id" : $num," /path/to/file


I get a lot of mileage out of grep's handy -A, -B, and -C options, the mnemonics for which I use are "After", "Before", and "Context".



  • grep -A n 'pattern' file will show the n lines After a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines Before a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines both before and after (i. e. the Context of) a pattern match, in addition to the matching line itself.






share|improve this answer






















  • For less toothpicks: grep -A1 '"id" : '"$num," ...
    – Kusalananda
    Mar 5 at 18:50






  • 1




    Just as many toothpicks; they're just shorter (:
    – DopeGhoti
    Mar 5 at 18:55














up vote
1
down vote



accepted










Given the shell variable num set equal to 335:



grep -A1 ""id" : $num," /path/to/file


I get a lot of mileage out of grep's handy -A, -B, and -C options, the mnemonics for which I use are "After", "Before", and "Context".



  • grep -A n 'pattern' file will show the n lines After a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines Before a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines both before and after (i. e. the Context of) a pattern match, in addition to the matching line itself.






share|improve this answer






















  • For less toothpicks: grep -A1 '"id" : '"$num," ...
    – Kusalananda
    Mar 5 at 18:50






  • 1




    Just as many toothpicks; they're just shorter (:
    – DopeGhoti
    Mar 5 at 18:55












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Given the shell variable num set equal to 335:



grep -A1 ""id" : $num," /path/to/file


I get a lot of mileage out of grep's handy -A, -B, and -C options, the mnemonics for which I use are "After", "Before", and "Context".



  • grep -A n 'pattern' file will show the n lines After a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines Before a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines both before and after (i. e. the Context of) a pattern match, in addition to the matching line itself.






share|improve this answer














Given the shell variable num set equal to 335:



grep -A1 ""id" : $num," /path/to/file


I get a lot of mileage out of grep's handy -A, -B, and -C options, the mnemonics for which I use are "After", "Before", and "Context".



  • grep -A n 'pattern' file will show the n lines After a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines Before a pattern match, in addition to the matching line itself.


  • grep -C n 'pattern' file will show the n lines both before and after (i. e. the Context of) a pattern match, in addition to the matching line itself.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 5 at 18:21









Kusalananda

103k13202318




103k13202318










answered Mar 5 at 17:17









DopeGhoti

40.2k54779




40.2k54779











  • For less toothpicks: grep -A1 '"id" : '"$num," ...
    – Kusalananda
    Mar 5 at 18:50






  • 1




    Just as many toothpicks; they're just shorter (:
    – DopeGhoti
    Mar 5 at 18:55
















  • For less toothpicks: grep -A1 '"id" : '"$num," ...
    – Kusalananda
    Mar 5 at 18:50






  • 1




    Just as many toothpicks; they're just shorter (:
    – DopeGhoti
    Mar 5 at 18:55















For less toothpicks: grep -A1 '"id" : '"$num," ...
– Kusalananda
Mar 5 at 18:50




For less toothpicks: grep -A1 '"id" : '"$num," ...
– Kusalananda
Mar 5 at 18:50




1




1




Just as many toothpicks; they're just shorter (:
– DopeGhoti
Mar 5 at 18:55




Just as many toothpicks; they're just shorter (:
– DopeGhoti
Mar 5 at 18:55












up vote
6
down vote













Assuming that the JSON file is one big array of objects such as



 
"href": "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests": "id": 333, "cluster_name": "sys41", "request_status": "COMPLETED"
,


(noting that newlines are not significant, and neither is the ordering of the keys in an object) then, the following jq command would get the request_status entry for the id corresponding to $num:



$ num=355
$ jq -r "..Requests | select(.id == $num).request_status" file.json
FAILED


It does this by picking out all the Requests entries from each object and filtering them on the id. For the ones that pass the filter, it extracts request_status.



The -r option to jq makes it output raw data instead of JSON (FAILED would have been double quoted without it).




Bonus material:



$ jq -r '..Requests | "(.id): (.request_status)"' file.json
333: COMPLETED
334: FAILED
335: FAILED
336: COMPLETED





share|improve this answer


















  • 2




    jq is the right tool for the job.
    – Zachary Brady
    Mar 5 at 18:36














up vote
6
down vote













Assuming that the JSON file is one big array of objects such as



 
"href": "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests": "id": 333, "cluster_name": "sys41", "request_status": "COMPLETED"
,


(noting that newlines are not significant, and neither is the ordering of the keys in an object) then, the following jq command would get the request_status entry for the id corresponding to $num:



$ num=355
$ jq -r "..Requests | select(.id == $num).request_status" file.json
FAILED


It does this by picking out all the Requests entries from each object and filtering them on the id. For the ones that pass the filter, it extracts request_status.



The -r option to jq makes it output raw data instead of JSON (FAILED would have been double quoted without it).




Bonus material:



$ jq -r '..Requests | "(.id): (.request_status)"' file.json
333: COMPLETED
334: FAILED
335: FAILED
336: COMPLETED





share|improve this answer


















  • 2




    jq is the right tool for the job.
    – Zachary Brady
    Mar 5 at 18:36












up vote
6
down vote










up vote
6
down vote









Assuming that the JSON file is one big array of objects such as



 
"href": "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests": "id": 333, "cluster_name": "sys41", "request_status": "COMPLETED"
,


(noting that newlines are not significant, and neither is the ordering of the keys in an object) then, the following jq command would get the request_status entry for the id corresponding to $num:



$ num=355
$ jq -r "..Requests | select(.id == $num).request_status" file.json
FAILED


It does this by picking out all the Requests entries from each object and filtering them on the id. For the ones that pass the filter, it extracts request_status.



The -r option to jq makes it output raw data instead of JSON (FAILED would have been double quoted without it).




Bonus material:



$ jq -r '..Requests | "(.id): (.request_status)"' file.json
333: COMPLETED
334: FAILED
335: FAILED
336: COMPLETED





share|improve this answer














Assuming that the JSON file is one big array of objects such as



 
"href": "http://localhost:8080/api/v1/clusters/sys41/requests/333",
"Requests": "id": 333, "cluster_name": "sys41", "request_status": "COMPLETED"
,


(noting that newlines are not significant, and neither is the ordering of the keys in an object) then, the following jq command would get the request_status entry for the id corresponding to $num:



$ num=355
$ jq -r "..Requests | select(.id == $num).request_status" file.json
FAILED


It does this by picking out all the Requests entries from each object and filtering them on the id. For the ones that pass the filter, it extracts request_status.



The -r option to jq makes it output raw data instead of JSON (FAILED would have been double quoted without it).




Bonus material:



$ jq -r '..Requests | "(.id): (.request_status)"' file.json
333: COMPLETED
334: FAILED
335: FAILED
336: COMPLETED






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 5 at 18:24

























answered Mar 5 at 17:45









Kusalananda

103k13202318




103k13202318







  • 2




    jq is the right tool for the job.
    – Zachary Brady
    Mar 5 at 18:36












  • 2




    jq is the right tool for the job.
    – Zachary Brady
    Mar 5 at 18:36







2




2




jq is the right tool for the job.
– Zachary Brady
Mar 5 at 18:36




jq is the right tool for the job.
– Zachary Brady
Mar 5 at 18:36










up vote
1
down vote













awk '/"id" : 335/getline;print'


or



var=""id" : 335"
awk "/$var/getline;print"


or



awk -v var=""id" : 335" '$0~vargetline;print'


or



var=""id" : 335"
awk -v var="$var" '$0~vargetline;print'


or any of the above, but without needing the "id" : " part of the string, in the form:



awk -v var="335" '$0~"id" : vargetline;print'





share|improve this answer






















  • May not work as intended. For example, will show a false match for "id" : 1355.
    – DopeGhoti
    Mar 5 at 17:24










  • Did you try that and have it fail?
    – user1404316
    Mar 5 at 17:27










  • Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
    – user1404316
    Mar 5 at 17:29














up vote
1
down vote













awk '/"id" : 335/getline;print'


or



var=""id" : 335"
awk "/$var/getline;print"


or



awk -v var=""id" : 335" '$0~vargetline;print'


or



var=""id" : 335"
awk -v var="$var" '$0~vargetline;print'


or any of the above, but without needing the "id" : " part of the string, in the form:



awk -v var="335" '$0~"id" : vargetline;print'





share|improve this answer






















  • May not work as intended. For example, will show a false match for "id" : 1355.
    – DopeGhoti
    Mar 5 at 17:24










  • Did you try that and have it fail?
    – user1404316
    Mar 5 at 17:27










  • Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
    – user1404316
    Mar 5 at 17:29












up vote
1
down vote










up vote
1
down vote









awk '/"id" : 335/getline;print'


or



var=""id" : 335"
awk "/$var/getline;print"


or



awk -v var=""id" : 335" '$0~vargetline;print'


or



var=""id" : 335"
awk -v var="$var" '$0~vargetline;print'


or any of the above, but without needing the "id" : " part of the string, in the form:



awk -v var="335" '$0~"id" : vargetline;print'





share|improve this answer














awk '/"id" : 335/getline;print'


or



var=""id" : 335"
awk "/$var/getline;print"


or



awk -v var=""id" : 335" '$0~vargetline;print'


or



var=""id" : 335"
awk -v var="$var" '$0~vargetline;print'


or any of the above, but without needing the "id" : " part of the string, in the form:



awk -v var="335" '$0~"id" : vargetline;print'






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 5 at 17:35

























answered Mar 5 at 17:18









user1404316

2,314520




2,314520











  • May not work as intended. For example, will show a false match for "id" : 1355.
    – DopeGhoti
    Mar 5 at 17:24










  • Did you try that and have it fail?
    – user1404316
    Mar 5 at 17:27










  • Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
    – user1404316
    Mar 5 at 17:29
















  • May not work as intended. For example, will show a false match for "id" : 1355.
    – DopeGhoti
    Mar 5 at 17:24










  • Did you try that and have it fail?
    – user1404316
    Mar 5 at 17:27










  • Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
    – user1404316
    Mar 5 at 17:29















May not work as intended. For example, will show a false match for "id" : 1355.
– DopeGhoti
Mar 5 at 17:24




May not work as intended. For example, will show a false match for "id" : 1355.
– DopeGhoti
Mar 5 at 17:24












Did you try that and have it fail?
– user1404316
Mar 5 at 17:27




Did you try that and have it fail?
– user1404316
Mar 5 at 17:27












Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
– user1404316
Mar 5 at 17:29




Oh, I see - you mean that I just pulled the rong text for the search. That's easy to replace.
– user1404316
Mar 5 at 17:29












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428323%2fextract-json-array-element-based-on-a-subelement-value%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?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay