Forum Discussion

Sona's avatar
Sona
New Contributor
7 years ago

I am using SOAPUI Open source.How to compare entire JSON response by checking every attribute

Exactly my question is i want to compare my entire json response array to another response array is same or not .

Every attribute should get match and not the values for the attributes.

No need to consider about my attributes values.I want to check in these two json array all attributes are same or not.

 

Can anyone please help me.

  • Lucian's avatar
    Lucian
    Community Hero

    Hi,

     

    I created a small piece of code which seems to work:

     

    import groovy.json.*
    
    // Define lists to hold all keys
    def firstJsonList = [] 
    def secondJsonList = []
    def thirdJsonList = []
    
    // Create a jsonSlurper object
    def jsonSlurper = new JsonSlurper()
    
    // Parse responses
    def firstJson = jsonSlurper.parseText( context.expand('${GetFirstJson#Response}') )
    def secondJson = jsonSlurper.parseText( context.expand('${GetSecondJson#Response}') )
    def thirdJson = jsonSlurper.parseText( context.expand('${GetThirdJson#Response}') )
    
    // Put all the keys in lists
    firstJson.each {
    	entry -> firstJsonList.add( "$entry.key" )
    }
    secondJson.each {
    	entry -> secondJsonList.add( "$entry.key" )
    }
    thirdJson.each {
    	entry -> thirdJsonList.add( "$entry.key" )	
    }
    
    // Compare the first and second list
    for (int i = 0; i < firstJsonList.size(); i++) {
    	assert ( firstJsonList[i] == secondJsonList[i] )
    }
    
    // Compare the first and third list
    for (int i = 0; i < firstJsonList.size(); i++) {
    	assert ( firstJsonList[i] == thirdJsonList[i] )
    }

    You can download the whole project from https://github.com/lucadln/soapui/tree/master/SoapUIOS/ModifyXmlInGroovy

     

    Cheers :robotvery-happy:

  • nmrao's avatar
    nmrao
    Champion Level 3
    Would you mind showing the sample data?