How to convert an Object to JSON in Groovy?

Views: 56 Ask New Question


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

On By

1 Answer


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"}
On By


Answer This Question