0
votes

import com.jayway.jsonpath.JsonPath

def path = vars.get("BaseFilePath") + "/" + vars.get("FhirVersion") + "/Get/Patient/";
def newLine = System.getProperty('line.separator')
def response = prev.getResponseDataAsString()

//address
def addressCSV = new File(path + 'address.csv')
def addressList = []
def addressCityCSV = new File(path + 'address-city.csv')
def cityList = []
def addressCountryCSV = new File(path + 'address-country.csv')
def countryList = []
def addressPostalCodeCSV = new File(path + 'address-postalcode.csv')
def postalCodeList = []
def addressStateCSV = new File(path + 'address-state.csv')
def stateList = []

def addressArray = JsonPath.read(response, '$..address')
addressArray.each { eachAddress ->
    eachAddress.each { subAddress ->
        subAddress.get('line').each { line ->
            addressList.add(line)
        }
		
		cityList.add(subAddress.get('city'))
		stateList.add(subAddress.get('state'))
		postalCodeList.add(subAddress.get('postalCode'))
		countryList(subAddress.get('country'))				
    }

addressList.unique().each { address ->
	addressCSV << address << newLine 
	}
cityList.unique().each { city ->
	addressCityCSV << city << newLine 
	}
countryList.unique().each { country ->
	addressCountryCSV << country << newLine
	}
postalCodeList.unique().each { postalCode ->
	addressPostalCodeCSV << postalCode << newLine
	}
stateList.unique().each { state ->
	addressStateCSV << state << newLine
	}
}

I wrote this scirpt in JSR223 post processor of jmeter, to extract data from json response, addressList, cityList and other lists contain duplicate elements so i wanted to remove the duplicates and push unique values into files. But this code is not working. Can someone help me in fixing this

2
Did you meant to accept a different answer?user7294900
answer from dimitri T works for mevenkat sai

2 Answers

0
votes

Calling Collection.unique() function should do the trick for you, i.e.

addressList = addressList.unique()

Demo:

enter image description here

However if your collection contains custom objects (i.e. not normal Strings) you will need to come up with a special Comparator implementation, i.e.

def myComparator = [
        equals: { delegate.equals(it) },
        compare: { source, target ->
            source.someField <=> target.someField
        }
] as Comparator

def unique = addressList.unique(myComparator)

See The Groovy Templates Cheat Sheet for JMeter for more Groovy tips and tricks

0
votes

For keeping only unique values use Set

We can use the toSet() function to convert a List to a Set.

So you can add it on the relevant lists as:

 uniqueStateCSVSet = addressStateCSV.toSet()

Or predefine it, as

 Set addressArray