rg2018
7 years agoOccasional Contributor
Help with groovy script
According to the log, parsedJson.criteriaList[count] contains
INFO:Combined: [criteriaName:Impact_On_Functioning, outcome:Met]}
and I want to match "Impact_On_Functioning, outcome:Met" however...
- 7 years ago
Use
case ~/.*Impact_On_Functioning, outcome=Met.*/:
When you are using "case" with a regex pattern, Groovy will invoke Pattern.isCase(String) method. Looks like the implementation must rely on toString(), while your log statement using ${ } syntax is relying on as String.
String json = '''{ "criteriaList": [ { "criteriaName": "Impact_On_Functioning", "outcome": "Met" } ] }''' def parsedJson = new groovy.json.JsonSlurper().parseText(json) assert (parsedJson.criteriaList[0] as String) == '[criteriaName:Impact_On_Functioning, outcome:Met]' assert "${parsedJson.criteriaList[0]}" == (parsedJson.criteriaList[0] as String) assert parsedJson.criteriaList[0] ==~ ~/.*Impact_On_Functioning, outcome:Met.*/ assert parsedJson.criteriaList[0].toString() == '{criteriaName=Impact_On_Functioning, outcome=Met}' assert parsedJson.criteriaList[0].toString() ==~ ~/.*Impact_On_Functioning, outcome=Met.*/ assert (~/.*Impact_On_Functioning, outcome=Met.*/).isCase( parsedJson.criteriaList[0] ) switch (parsedJson.criteriaList[0]) { case ~/.*Impact_On_Functioning, outcome=Met.*/: break default: assert false break }
Also, consider whether something like this is simpler for you than using Regex at all.
def expectedResults = [ [criteriaName: "Impact_On_Functioning", outcome: "Met"], [criteriaName: "Diagnostic_Determination", outcome: "Met"], [criteriaName: "Specialized_Support_Need", outcome: "Met"] //... ] expectedResults.each { assert it in parsedJson.criteriaList }