'catch groovy JsonSlurper parseText exception

groovy> def d = '{"a": 1, "b": {"bb": 2}}, "c": 3}' 
groovy> def m = new groovy.json.JsonSlurper().parseText(d) 
groovy> println m 
groovy> println m instanceof Map 
 
[a:1, b:[bb:2]]
true

Obviously there is a redundant } at key b, but groovy can also parse, and key c lost. How to throw exception?



Solution 1:[1]

unfortunately it's a feature of groovy json slurper...

the only way i found as workaround - when you want to validate json without external json parser:

import groovy.json.*
def d = '{"a": 1, "b": {"bb": 2}}, "c": 3}'
def m = new JsonSlurper().parseText('['+d+']')[0] //parse as array and get first element

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 daggett