0

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)
GPs
  • 67
  • 1
  • 10
  • The error is due to iterating over a map. Most likely `result` does not contain, what you think it contains (e.g. the json is not an array but an object at top level). – cfrick Feb 23 '21 at 19:59
  • result do have the ` "Fieldid": "Fieldid/112", "fieldName": "TX.Sessionval.cost", "stringval": "jklah-dw-4c8d-8320-das313s3ASsda|000725N8WuUrfwAS7alP|banker_name" ` under newFields[112] but it changed every time so instead this, need to search whole JSON output with katalon. – GPs Feb 23 '21 at 20:38
  • 1
    so, your `result` contains just one map without wrapping array. truthfully i cant understand what you want to achieve.. but if you want to remove all keys from map where value does not contain 'TX...' : `result.findAll{ ! it.value.contains("TX.Sessionval.cost") }` – daggett Feb 23 '21 at 20:40
  • Your example JSON is a **map** inside a **list**. And the error you are getting is that an element of `result` is a **map entry** - so clearly there is a discrepancy between the JSON you are sharing and JSON you are actually reading. – cfrick Feb 23 '21 at 20:41
  • JSON result is huge, just looking out the solution based on what i am using for `postman.setEnvironmentVariable("Stdid", jsonData.newFields[112].stringValue)` but since the value gets changed everytime like Fieldid": "Fieldid/112, Fieldid": "Fieldid/123, Fieldid": "Fieldid/129 then have to manually edit it every time rather filtering to specific search When fieldName": "TX.Sessionval.cost" is this print the stringvalue – GPs Feb 23 '21 at 20:46
  • If you reduce your problem to something, that no longer represents your actual problem, then you wont get any good answers because they will answer the wrong question. – cfrick Feb 23 '21 at 20:48

2 Answers2

2

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

result.newFields.find{ it.fieldName == "TX.Sessionval.cost" }?.stringValue
cfrick
  • 35,203
  • 6
  • 56
  • 68
  • @cfick, what to update in question to be more meaningful, update the same – GPs Feb 23 '21 at 20:50
  • console gives me null when printed, just to add when checked Json path it gives me x.newFields[112].stringValue. – GPs Feb 23 '21 at 21:05
  • customFields(filter((a)==a.fieldName=='TX.Sessionval.cost')[0].stringValue)) this works in postman – GPs Feb 23 '21 at 21:09
  • First `newFields` worked and now `customFields` works... have you tried applying that to my answer? – cfrick Feb 24 '21 at 07:23
  • Yes your solution works in postman. but it doesn't works with groovy `newFields` ignore customfield sorry. – GPs Feb 24 '21 at 15:06
  • This works with me `result.newFields.findAll { Map map -> map.get("fieldName").contains("TX.Sessionval.cost") }?.stringValue` – GPs Feb 24 '21 at 16:30
0

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

GPs
  • 67
  • 1
  • 10