Hi Jim,

You should just be able to catch the Exception.

['3.5', '3.5.5', '['].each {
  try {
    new JsonSlurper().parseText(it)
    println 'Valid'
  } catch(error) {
    println "Invalid: $error.message"
  }
}

There are some more examples in the unit tests, e.g.:
https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/test/groovy/groovy/json/JsonSlurperTest.groovy#L260-L265

Cheers, Paul.

On Tue, Sep 21, 2021 at 8:35 PM James McMahon <jsmcmah...@gmail.com> wrote:

> Hello. Newbie to Groovy. Have a text string that I need to verify is a
> representation of valid JSON, or not. What is an effective means to do this
> in Groovy? I'm having difficulty determining what a method like JSONSlurper
> will return to me if the string can't be parsed because it is not valid
> JSON? I've found plenty of examples of results it returns when it works,
> but nothing yet showing me what it returns when the string isn't valid
> JSON.
> Hope this is a valid way to post such a question. As I said, this is my
> first time trying this or any Groovy forum. Thanks in advance for any help.
> -Jim
>
>

Reply via email to