Groovy web console

subscribe to the feed Subscribe
to this
site
query matching unmatching property (via #groovywebconsole)
tweet this snippet Tweet
this
script

query matching unmatching property

Published 5 months ago by nmrao with tags matching unmatching
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def json = '''{
"results": [
{"documentNumber": "112069353213670593"},
{"documentNumber": "120212364882313025"},
{"documentNumber": "120055742821186593"},
{"documentNumber": "123134986738657857"},
{"documentNumber": "123207962820539585"},
{"documentNumber": "1234567"},
{"documentNumber": "123456789"},
{"documentNumber": "123597020827608033"},
{"documentNumber": "123639294264534913"}
]
}'''
def expectedPattern = '123'
def joinData = { items -> items?.join(', ') }
def printMessage = { msg, items -> !items.size() ?: println("${msg} : ${joinData(items)}") }
def parsedJson = new groovy.json.JsonSlurper().parseText(json)
def getFilteredDocuments = { closure -> parsedJson.results.documentNumber.findAll(closure) }
def matchingDocuments = getFilteredDocuments { it.contains expectedPattern }
def unwantedDocuments = getFilteredDocuments { !it.contains(expectedPattern) }
def allDocuments = getFilteredDocuments { it }
printMessage 'Total document numbers', allDocuments
printMessage 'Matching document numbers', matchingDocuments
printMessage 'Unwanted document numbers', unwantedDocuments
assert !unwantedDocuments.size(), "There are invalid document numbers found : ${joinData(unwantedDocuments)}"