GPs
GPs

Reputation: 85

Filtering/Selecting array values from index in Json in groovy

Trying to filter selective index. Fieldid values changes with every build, What do not change is fieldName": "TX.Sessionval.cost". Need to filter out the whole stringval and save into variable

fieldName": "TX.Sessionval.cost"
[
  {
   "Fieldid": "Fieldid/112",
    "fieldName": "TX.Sessionval.cost",
    "stringval": "jklah-dw-4c8d-8320-das313s3ASsda|000725N8WuUrfwAS7alP|banker_name"
  }
]

tried def slurper = new JsonSlurper() def result = slurper.parseText(response.getResponseBodyContent()) def newf = result.findAll { it.contains("TX.Sessionval.cost") } getting groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap$Entry.contains()

in Postman locator works fine

postman.setEnvironmentVariable("Stdid", jsonData.newFields[112].stringValue)

Upvotes: 0

Views: 1311

Answers (2)

GPs
GPs

Reputation: 85

This works with me result.newFields.findAll { Map map -> map.get("fieldName").contains("TX.Sessionval.cost") }?.stringValue

Upvotes: 0

cfrick
cfrick

Reputation: 37008

Your JSON in your question is wrong. Extrapolating from your working code, this should work:

result.newFields.find{ it.fieldName == "TX.Sessionval.cost" }?.stringValue

Upvotes: 2

Related Questions