Extract json array element based on a subelement value
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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"
text-processing json jq
add a comment |Â
up vote
2
down vote
favorite
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"
text-processing json jq
3
This looks like a job forjq
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 properjq
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
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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"
text-processing json jq
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"
text-processing json jq
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 forjq
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 properjq
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
add a comment |Â
3
This looks like a job forjq
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 properjq
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
add a comment |Â
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.
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
add a comment |Â
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
2
jq is the right tool for the job.
â Zachary Brady
Mar 5 at 18:36
add a comment |Â
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'
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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
2
jq is the right tool for the job.
â Zachary Brady
Mar 5 at 18:36
add a comment |Â
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
2
jq is the right tool for the job.
â Zachary Brady
Mar 5 at 18:36
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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'
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
add a comment |Â
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'
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
add a comment |Â
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'
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'
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
add a comment |Â
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
add a comment |Â
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
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
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
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
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
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