How to verify the returned JSON response is in sorting order?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have an API to fetch the list of employee Name's in an organization and it supports order by clause.
I invoked an API "get /employeeName?$ordeyby=name desc". I got the results like below,



{
"value":[
{
"name":"Sam"
},
{
"name":"Peter"
},
{
"name":"Harry"
},
{
"name":"Arnold"
}]
}


I have parsed each name and stored into a variable of string type.



How can I verify using JAVA Script/BeanShell/Groovy, that the returned response is in descending order?



Can anyone please help here. Any of the above-mentioned languages is fine and I want this needs to be implemented in JMeter.



Thanks in advance.










share|improve this question





























    1















    I have an API to fetch the list of employee Name's in an organization and it supports order by clause.
    I invoked an API "get /employeeName?$ordeyby=name desc". I got the results like below,



    {
    "value":[
    {
    "name":"Sam"
    },
    {
    "name":"Peter"
    },
    {
    "name":"Harry"
    },
    {
    "name":"Arnold"
    }]
    }


    I have parsed each name and stored into a variable of string type.



    How can I verify using JAVA Script/BeanShell/Groovy, that the returned response is in descending order?



    Can anyone please help here. Any of the above-mentioned languages is fine and I want this needs to be implemented in JMeter.



    Thanks in advance.










    share|improve this question

























      1












      1








      1








      I have an API to fetch the list of employee Name's in an organization and it supports order by clause.
      I invoked an API "get /employeeName?$ordeyby=name desc". I got the results like below,



      {
      "value":[
      {
      "name":"Sam"
      },
      {
      "name":"Peter"
      },
      {
      "name":"Harry"
      },
      {
      "name":"Arnold"
      }]
      }


      I have parsed each name and stored into a variable of string type.



      How can I verify using JAVA Script/BeanShell/Groovy, that the returned response is in descending order?



      Can anyone please help here. Any of the above-mentioned languages is fine and I want this needs to be implemented in JMeter.



      Thanks in advance.










      share|improve this question














      I have an API to fetch the list of employee Name's in an organization and it supports order by clause.
      I invoked an API "get /employeeName?$ordeyby=name desc". I got the results like below,



      {
      "value":[
      {
      "name":"Sam"
      },
      {
      "name":"Peter"
      },
      {
      "name":"Harry"
      },
      {
      "name":"Arnold"
      }]
      }


      I have parsed each name and stored into a variable of string type.



      How can I verify using JAVA Script/BeanShell/Groovy, that the returned response is in descending order?



      Can anyone please help here. Any of the above-mentioned languages is fine and I want this needs to be implemented in JMeter.



      Thanks in advance.







      javascript groovy jmeter beanshell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 14:28









      HariHari

      577




      577
























          2 Answers
          2






          active

          oldest

          votes


















          1















          1. Add JSR223 Assertion as a child of the request which returns the above JSON


          2. Put the following code into "Script" area:



            def expected = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..name').sort().reverse()

            new groovy.json.JsonSlurper().parse(prev.getResponseData()).value.eachWithIndex { def entry, int i ->
            if (!entry.name.equals(expected.get(i))) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('Order mismatch, expected: ' + expected.get(i) + ', got: ' + entry.name)
            }
            }


          3. That's it, in case of expected alphabetical descending order the sampler will be successful, otherwise you will get an error message indicating which name is expected and what is the actual one


          More information:




          • Jayway JsonPath

          • Groovy Parsing and Producing JSON

          • Scripting JMeter Assertions in Groovy - A Tutorial






          share|improve this answer
























          • This solution helped me. Thanks.

            – Hari
            Nov 28 '18 at 6:11





















          1














          You can sort it in js using this approach



          var employers = [
          {
          "name":"Sam"
          },
          {
          "name":"Peter"
          },
          {
          "name":"Harry"
          },
          {
          "name":"Arnold"
          }];

          console.log(employers.sort(function(e1,e2){
          var alc = e1.name.toLowerCase(), blc = e2.name.toLowerCase();
          return alc > blc ? 1 : alc < blc ? -1 : 0;
          }));





          share|improve this answer
























          • I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

            – Jonathan Rys
            Nov 23 '18 at 15:01











          • makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

            – dganenco
            Nov 23 '18 at 15:03











          • API returns the response in a sorted manner, I just want to verify the sorting of return response.

            – Hari
            Nov 23 '18 at 15:24











          • @Hari Then I suppose you could sort the response, and then compare it to the original response?

            – billjamesdev
            Nov 24 '18 at 16:45











          • @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

            – billjamesdev
            Nov 26 '18 at 4:05












          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',
          autoActivateHeartbeat: false,
          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%2f53448507%2fhow-to-verify-the-returned-json-response-is-in-sorting-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          1. Add JSR223 Assertion as a child of the request which returns the above JSON


          2. Put the following code into "Script" area:



            def expected = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..name').sort().reverse()

            new groovy.json.JsonSlurper().parse(prev.getResponseData()).value.eachWithIndex { def entry, int i ->
            if (!entry.name.equals(expected.get(i))) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('Order mismatch, expected: ' + expected.get(i) + ', got: ' + entry.name)
            }
            }


          3. That's it, in case of expected alphabetical descending order the sampler will be successful, otherwise you will get an error message indicating which name is expected and what is the actual one


          More information:




          • Jayway JsonPath

          • Groovy Parsing and Producing JSON

          • Scripting JMeter Assertions in Groovy - A Tutorial






          share|improve this answer
























          • This solution helped me. Thanks.

            – Hari
            Nov 28 '18 at 6:11


















          1















          1. Add JSR223 Assertion as a child of the request which returns the above JSON


          2. Put the following code into "Script" area:



            def expected = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..name').sort().reverse()

            new groovy.json.JsonSlurper().parse(prev.getResponseData()).value.eachWithIndex { def entry, int i ->
            if (!entry.name.equals(expected.get(i))) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('Order mismatch, expected: ' + expected.get(i) + ', got: ' + entry.name)
            }
            }


          3. That's it, in case of expected alphabetical descending order the sampler will be successful, otherwise you will get an error message indicating which name is expected and what is the actual one


          More information:




          • Jayway JsonPath

          • Groovy Parsing and Producing JSON

          • Scripting JMeter Assertions in Groovy - A Tutorial






          share|improve this answer
























          • This solution helped me. Thanks.

            – Hari
            Nov 28 '18 at 6:11
















          1












          1








          1








          1. Add JSR223 Assertion as a child of the request which returns the above JSON


          2. Put the following code into "Script" area:



            def expected = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..name').sort().reverse()

            new groovy.json.JsonSlurper().parse(prev.getResponseData()).value.eachWithIndex { def entry, int i ->
            if (!entry.name.equals(expected.get(i))) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('Order mismatch, expected: ' + expected.get(i) + ', got: ' + entry.name)
            }
            }


          3. That's it, in case of expected alphabetical descending order the sampler will be successful, otherwise you will get an error message indicating which name is expected and what is the actual one


          More information:




          • Jayway JsonPath

          • Groovy Parsing and Producing JSON

          • Scripting JMeter Assertions in Groovy - A Tutorial






          share|improve this answer














          1. Add JSR223 Assertion as a child of the request which returns the above JSON


          2. Put the following code into "Script" area:



            def expected = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..name').sort().reverse()

            new groovy.json.JsonSlurper().parse(prev.getResponseData()).value.eachWithIndex { def entry, int i ->
            if (!entry.name.equals(expected.get(i))) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('Order mismatch, expected: ' + expected.get(i) + ', got: ' + entry.name)
            }
            }


          3. That's it, in case of expected alphabetical descending order the sampler will be successful, otherwise you will get an error message indicating which name is expected and what is the actual one


          More information:




          • Jayway JsonPath

          • Groovy Parsing and Producing JSON

          • Scripting JMeter Assertions in Groovy - A Tutorial







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 16:41









          Dmitri TDmitri T

          74.9k33766




          74.9k33766













          • This solution helped me. Thanks.

            – Hari
            Nov 28 '18 at 6:11





















          • This solution helped me. Thanks.

            – Hari
            Nov 28 '18 at 6:11



















          This solution helped me. Thanks.

          – Hari
          Nov 28 '18 at 6:11







          This solution helped me. Thanks.

          – Hari
          Nov 28 '18 at 6:11















          1














          You can sort it in js using this approach



          var employers = [
          {
          "name":"Sam"
          },
          {
          "name":"Peter"
          },
          {
          "name":"Harry"
          },
          {
          "name":"Arnold"
          }];

          console.log(employers.sort(function(e1,e2){
          var alc = e1.name.toLowerCase(), blc = e2.name.toLowerCase();
          return alc > blc ? 1 : alc < blc ? -1 : 0;
          }));





          share|improve this answer
























          • I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

            – Jonathan Rys
            Nov 23 '18 at 15:01











          • makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

            – dganenco
            Nov 23 '18 at 15:03











          • API returns the response in a sorted manner, I just want to verify the sorting of return response.

            – Hari
            Nov 23 '18 at 15:24











          • @Hari Then I suppose you could sort the response, and then compare it to the original response?

            – billjamesdev
            Nov 24 '18 at 16:45











          • @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

            – billjamesdev
            Nov 26 '18 at 4:05
















          1














          You can sort it in js using this approach



          var employers = [
          {
          "name":"Sam"
          },
          {
          "name":"Peter"
          },
          {
          "name":"Harry"
          },
          {
          "name":"Arnold"
          }];

          console.log(employers.sort(function(e1,e2){
          var alc = e1.name.toLowerCase(), blc = e2.name.toLowerCase();
          return alc > blc ? 1 : alc < blc ? -1 : 0;
          }));





          share|improve this answer
























          • I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

            – Jonathan Rys
            Nov 23 '18 at 15:01











          • makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

            – dganenco
            Nov 23 '18 at 15:03











          • API returns the response in a sorted manner, I just want to verify the sorting of return response.

            – Hari
            Nov 23 '18 at 15:24











          • @Hari Then I suppose you could sort the response, and then compare it to the original response?

            – billjamesdev
            Nov 24 '18 at 16:45











          • @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

            – billjamesdev
            Nov 26 '18 at 4:05














          1












          1








          1







          You can sort it in js using this approach



          var employers = [
          {
          "name":"Sam"
          },
          {
          "name":"Peter"
          },
          {
          "name":"Harry"
          },
          {
          "name":"Arnold"
          }];

          console.log(employers.sort(function(e1,e2){
          var alc = e1.name.toLowerCase(), blc = e2.name.toLowerCase();
          return alc > blc ? 1 : alc < blc ? -1 : 0;
          }));





          share|improve this answer













          You can sort it in js using this approach



          var employers = [
          {
          "name":"Sam"
          },
          {
          "name":"Peter"
          },
          {
          "name":"Harry"
          },
          {
          "name":"Arnold"
          }];

          console.log(employers.sort(function(e1,e2){
          var alc = e1.name.toLowerCase(), blc = e2.name.toLowerCase();
          return alc > blc ? 1 : alc < blc ? -1 : 0;
          }));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 14:56









          dganencodganenco

          24618




          24618













          • I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

            – Jonathan Rys
            Nov 23 '18 at 15:01











          • makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

            – dganenco
            Nov 23 '18 at 15:03











          • API returns the response in a sorted manner, I just want to verify the sorting of return response.

            – Hari
            Nov 23 '18 at 15:24











          • @Hari Then I suppose you could sort the response, and then compare it to the original response?

            – billjamesdev
            Nov 24 '18 at 16:45











          • @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

            – billjamesdev
            Nov 26 '18 at 4:05



















          • I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

            – Jonathan Rys
            Nov 23 '18 at 15:01











          • makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

            – dganenco
            Nov 23 '18 at 15:03











          • API returns the response in a sorted manner, I just want to verify the sorting of return response.

            – Hari
            Nov 23 '18 at 15:24











          • @Hari Then I suppose you could sort the response, and then compare it to the original response?

            – billjamesdev
            Nov 24 '18 at 16:45











          • @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

            – billjamesdev
            Nov 26 '18 at 4:05

















          I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

          – Jonathan Rys
          Nov 23 '18 at 15:01





          I'd hate to inherit that code. Nested ternaries are a bear to read and your variable names are not very descriptive...

          – Jonathan Rys
          Nov 23 '18 at 15:01













          makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

          – dganenco
          Nov 23 '18 at 15:03





          makes sense, I provided example for you. If you hate ternaries you can use this return a.name.toLowerCase().localeCompare(b.name.toLowerCase());

          – dganenco
          Nov 23 '18 at 15:03













          API returns the response in a sorted manner, I just want to verify the sorting of return response.

          – Hari
          Nov 23 '18 at 15:24





          API returns the response in a sorted manner, I just want to verify the sorting of return response.

          – Hari
          Nov 23 '18 at 15:24













          @Hari Then I suppose you could sort the response, and then compare it to the original response?

          – billjamesdev
          Nov 24 '18 at 16:45





          @Hari Then I suppose you could sort the response, and then compare it to the original response?

          – billjamesdev
          Nov 24 '18 at 16:45













          @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

          – billjamesdev
          Nov 26 '18 at 4:05





          @Hari That's what we want.. for you to post the code you tried, and why you say it "didn't work".

          – billjamesdev
          Nov 26 '18 at 4:05


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53448507%2fhow-to-verify-the-returned-json-response-is-in-sorting-order%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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]