logo image

How to convert an Object to JSON in Groovy?


How to convert an Object to JSON in Groovy without using any third party library?

1 Answer

1 year ago by

You can achieve this natively in Groovy, without using any third-party library using groovy.json.JsonOutput.toJson() Following is the sample code

​def colors = [ red : '#FF0000',
               black : '#000000',
               white : '#FFFFFF' ]
def jsonString = groovy.json.JsonOutput.toJson(colors);

println jsonString

Output:

{"red":"#FF0000","black":"#000000","white":"#FFFFFF"}
1 year ago by Megamind