groupby an element with jq
up vote
0
down vote
favorite
I have the following json:
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d2"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d3"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d4"},"org":"AB KIO","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d5"},"org":"GH SVS","rc":{"$event":"17"}}
How could i achieve the following output result? (tsv)
13 TΙ UIH 2
13 AB KIO 1
17 GH SVS 1
so far from what i have searched,
jq -sr 'group_by(.org)|.|[.[0].org, length]|@tsv'
how could i add one more group_by to achieve the desired result?
jq
add a comment |
up vote
0
down vote
favorite
I have the following json:
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d2"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d3"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d4"},"org":"AB KIO","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d5"},"org":"GH SVS","rc":{"$event":"17"}}
How could i achieve the following output result? (tsv)
13 TΙ UIH 2
13 AB KIO 1
17 GH SVS 1
so far from what i have searched,
jq -sr 'group_by(.org)|.|[.[0].org, length]|@tsv'
how could i add one more group_by to achieve the desired result?
jq
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following json:
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d2"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d3"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d4"},"org":"AB KIO","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d5"},"org":"GH SVS","rc":{"$event":"17"}}
How could i achieve the following output result? (tsv)
13 TΙ UIH 2
13 AB KIO 1
17 GH SVS 1
so far from what i have searched,
jq -sr 'group_by(.org)|.|[.[0].org, length]|@tsv'
how could i add one more group_by to achieve the desired result?
jq
I have the following json:
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d2"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d3"},"org":"TΙ UIH","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d4"},"org":"AB KIO","rc":{"$event":"13"}}
{"us":{"$event":"5bbf4a4f43d8950b5b0cc6d5"},"org":"GH SVS","rc":{"$event":"17"}}
How could i achieve the following output result? (tsv)
13 TΙ UIH 2
13 AB KIO 1
17 GH SVS 1
so far from what i have searched,
jq -sr 'group_by(.org)|.|[.[0].org, length]|@tsv'
how could i add one more group_by to achieve the desired result?
jq
jq
edited Nov 9 at 20:00
Adrian W
1,67931320
1,67931320
asked Nov 9 at 16:15
meno
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I was able to obtain the expected result from your sample JSON using the following :
group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length] | @tsv
You can try it on jqplay.org.
The modification of the group_by
clause ensures we will have one entry by pair of .org
/.rc.$event
(without it we would only have one entry by .org
, which might hide some .rc.$event
).
Then we add the .rc.$event
to the array you create just as you did with the .org
, accessing the value of the first item of the array since we know they're all the same anyway.
To sort the result, you can put it in an array and use sort_by(.[0])
which will sort by the first element of the rows :
[group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length]] | sort_by(.[0]) | @tsv
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.sort_by(.[0], .[2])
to sort values with the same $event by record count
– Aaron
Nov 9 at 16:43
thank you very much
– meno
Nov 9 at 17:06
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I was able to obtain the expected result from your sample JSON using the following :
group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length] | @tsv
You can try it on jqplay.org.
The modification of the group_by
clause ensures we will have one entry by pair of .org
/.rc.$event
(without it we would only have one entry by .org
, which might hide some .rc.$event
).
Then we add the .rc.$event
to the array you create just as you did with the .org
, accessing the value of the first item of the array since we know they're all the same anyway.
To sort the result, you can put it in an array and use sort_by(.[0])
which will sort by the first element of the rows :
[group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length]] | sort_by(.[0]) | @tsv
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.sort_by(.[0], .[2])
to sort values with the same $event by record count
– Aaron
Nov 9 at 16:43
thank you very much
– meno
Nov 9 at 17:06
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
add a comment |
up vote
1
down vote
accepted
I was able to obtain the expected result from your sample JSON using the following :
group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length] | @tsv
You can try it on jqplay.org.
The modification of the group_by
clause ensures we will have one entry by pair of .org
/.rc.$event
(without it we would only have one entry by .org
, which might hide some .rc.$event
).
Then we add the .rc.$event
to the array you create just as you did with the .org
, accessing the value of the first item of the array since we know they're all the same anyway.
To sort the result, you can put it in an array and use sort_by(.[0])
which will sort by the first element of the rows :
[group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length]] | sort_by(.[0]) | @tsv
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.sort_by(.[0], .[2])
to sort values with the same $event by record count
– Aaron
Nov 9 at 16:43
thank you very much
– meno
Nov 9 at 17:06
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I was able to obtain the expected result from your sample JSON using the following :
group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length] | @tsv
You can try it on jqplay.org.
The modification of the group_by
clause ensures we will have one entry by pair of .org
/.rc.$event
(without it we would only have one entry by .org
, which might hide some .rc.$event
).
Then we add the .rc.$event
to the array you create just as you did with the .org
, accessing the value of the first item of the array since we know they're all the same anyway.
To sort the result, you can put it in an array and use sort_by(.[0])
which will sort by the first element of the rows :
[group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length]] | sort_by(.[0]) | @tsv
I was able to obtain the expected result from your sample JSON using the following :
group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length] | @tsv
You can try it on jqplay.org.
The modification of the group_by
clause ensures we will have one entry by pair of .org
/.rc.$event
(without it we would only have one entry by .org
, which might hide some .rc.$event
).
Then we add the .rc.$event
to the array you create just as you did with the .org
, accessing the value of the first item of the array since we know they're all the same anyway.
To sort the result, you can put it in an array and use sort_by(.[0])
which will sort by the first element of the rows :
[group_by(.org, .rc."$event") | [.[0].rc."$event", .[0].org, length]] | sort_by(.[0]) | @tsv
edited Nov 9 at 16:41
answered Nov 9 at 16:25
Aaron
14.6k11636
14.6k11636
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.sort_by(.[0], .[2])
to sort values with the same $event by record count
– Aaron
Nov 9 at 16:43
thank you very much
– meno
Nov 9 at 17:06
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
add a comment |
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.sort_by(.[0], .[2])
to sort values with the same $event by record count
– Aaron
Nov 9 at 16:43
thank you very much
– meno
Nov 9 at 17:06
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
thank you, could i also somehow sort values by $event value?
– meno
Nov 9 at 16:37
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.
sort_by(.[0], .[2])
to sort values with the same $event by record count– Aaron
Nov 9 at 16:43
@meno I've updated the answer to specify a sort order. Note that you can specify more than one, e.g.
sort_by(.[0], .[2])
to sort values with the same $event by record count– Aaron
Nov 9 at 16:43
thank you very much
– meno
Nov 9 at 17:06
thank you very much
– meno
Nov 9 at 17:06
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
You're welcome, glad I could help !
– Aaron
Nov 9 at 17:09
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229452%2fgroupby-an-element-with-jq%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