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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 20:00









      Adrian W

      1,67931320




      1,67931320










      asked Nov 9 at 16:15









      meno

      32




      32
























          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





          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53229452%2fgroupby-an-element-with-jq%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








          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





          share|improve this answer























          • 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















          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





          share|improve this answer























          • 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













          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff