7

My java.lang.String is of form

 [[{"ABC":{"total":0,"failed":0,"skipped":0}}], [{"BCD": {"total":0,"failed":0,"skipped":0}}]]

How to convert this to json in groovy?

3
  • 6
    That looks like JSON already... Commented Aug 27, 2018 at 18:33
  • maybe you want to parse the string to objects? what's your final goal? Commented Aug 27, 2018 at 19:49
  • Possible duplicate of how to parse json using groovy Commented Aug 28, 2018 at 7:58

1 Answer 1

14

Parsing json from string with built-in groovy tools is done with groovy.json.JsonSlurper. You can check the documentation at here.

Here's how your example json can be accessed, just like groovy nested map:

def str = '[[{"ABC":{"total":0,"failed":0,"skipped":0}}], [{"BCD": {"total":0,"failed":0,"skipped":0}}]]'
def parser = new JsonSlurper()
def json = parser.parseText(str)
assert json[0][0].ABC.total == 0
assert json[0][0].ABC.failed == 0
assert json[0][0].ABC.skipped == 0
assert json[1][0].BCD.total == 0
assert json[1][0].BCD.failed == 0
assert json[1][0].BCD.skipped == 0
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.