Reputation: 955
I am trying to remove empty elements i.e. double quotes from array but i am not sure how to iterate through json array. Below is my json
{
"body": {
"newId":"value1"
},
"header": {
"appId": "someVal",
"pricingSchedule": [
"price1",
"price2",
"price3",
"",
"",
""
]
},
"trail": {
"pageSize": "50"
}
}
what i want to do is in the header
i want to iterate through pricingSchedule
array and remove empty elements and if there are no elements in array then i want to simply keep empty array rather then removing it.
Below is my attempted code -
def request = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
def newRequest = evaluate(request.inspect())
request.body.each { entry ->
if (entry.getValue().equals('') || entry.getValue().equals([''])) {
newRequest.body.remove(entry.getKey())
}
}
request.header.each{ entry ->
// String key=entry.getKey().equals("pricingSchedule")
entry.getKey().equals("pricingSchedule").each{ entry1 ->
log.info(entry1) //This does not print anything and gives me error
}
}
def arguments = new org.apache.jmeter.config.Arguments()
sampler.setArguments(arguments)
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(newRequest).toPrettyString(), '')
sampler.setPostBodyRaw(true)
Upvotes: 1
Views: 2230
Reputation: 171084
To remove the empty elements from pricingSchedule
, just do:
request.header.pricingSchedule = request.header.pricingSchedule.findAll()
This will remove any elements that are null, or the empty string
Upvotes: 6