Forum Discussion

richie's avatar
richie
Community Hero
7 years ago

Compare a GUID attribute value from 2 different json responses?

Hey,

 

I don't know if I'm being an idiot - whether my mind's gone blank and there's a GUI way of doing this or whether I have to rely on groovy.

 

Anyway - I have 2 GET requests and these generate 2 .json responses

 

Within each of these 2 GET requests' responses, a unique GUID value is returned (format like e6de8a88-cad1-e611-80d2-3863bb361018) 

 

I need to compare the GUID values from each of these and assert that they are identical

 

The format of the 1st response is as follows:

 

[
   {
      "@odata.etag" : "W/\"4815016\"",
      "whatevs1" : 1,
      "whatevs2" : 1,
      "GUID" : "5ae7c359-e148-4ea2-a4b7-d5500aebe185",
 }
]

jsonpath to GUID in response == $[0]['GUID']

The format of the 2nd response is as follows:

 

{
   "@odata.context" : "https://whatevs.crm4.dynamics.com/api/data/v9.0/$metadata#GUID(GUID)",
   "value" : [
      {
         "@odata.etag" : "W/\"1569419\"",
         "GUID" : "5ae7c359-e148-4ea2-a4b7-d5500aebe185"
      }
   ]
}


jsonpath to GUID in second response: $['value'][0]['GUID']

 

 

Now I've began putting together the following groovy to compare and assert the two GUID values

 

import groovy.json.JsonSlurper



def Response1 = context.expand( '${#GetDonorID#Response#://$['value'][0]['GUID']}' ).toString()
GetDonorID = new JsonSlurper().parseText Response1



def Response2 = context.expand( '${#GetGUID#Response#://$[0]['GUID']}' ).toString()
GetGUID = new JsonSlurper().parseText Response2



assert GetDonorID == GetGUID

 

 

I'm  getting multiple compilation errors - which I'm assuming is cos I tried adding in the jsonpath to my context expand line - but I've seen this done using xpath using XMLSlurper so I thought this would work using the JsonSlurper.

 

Can anyone advise where I'm going wrong with this?

 

I was thinking I could pass both options to a Properties step and use 3 lines of groovy to compare and assert the 2 properties but I already know I can get the properties approach to work and I'm not going to expand my education doing this approach - hence the reason I'm trying this way instead!

 

Many thanks to any hints/tips/guidance etc.

 

richie

  • groovyguy's avatar
    groovyguy
    Community Hero

    I believe you were very close, but there's a slightly easier way. Here's an adjustment to your script:

     

    import groovy.json.JsonSlurper
    
     
    // Get the responses and convert to string
    def Response1 = context.expand( '${GetDonorID#Response}' ).toString();
    def Response2 = context.expand( '${GetGUID#Response}' ).toString();
    
    log.info(Response1);
    log.info(Response2);
    
    // Set up JSON parsers of the two responses
    def GetDonorID = new JsonSlurper().parseText(Response1);
    def GetGUID = new JsonSlurper().parseText(Response2);
    
    log.info(GetDonorID);
    log.info(GetGUID);
    
    // Use JSON to find the guids
    log.info (GetDonorID["GUID"]);
    log.info(GetGUID["value"]["GUID"]);
    
    // Compare the guids
    assert (GetDonorID["GUID"].equals(GetGUID["value"]["GUID"]));
    
  • nmrao's avatar
    nmrao
    Champion Level 3
    Looks there are arrays in the response.
    Do you have any matching criteria? such as compare the "GUID" only for matching value of "@odata.etag"?
    • richie's avatar
      richie
      Community Hero

      lads,

       

      thanks for your help - I'm being dragged onto something else - but I'll get to this on Monday,

       

      Cheers,

       

      richie

      • groovyguy's avatar
        groovyguy
        Community Hero

        I hope it works for what you want, richie. I might not be able to reply next week, I'll have limited internet/computer access. Good luck! :)