1

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)

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

This one liner did perfectly for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.