Home » 
        Code Examples » 
        Groovy Code Examples
    
        
    Groovy - Convert object to JSON string Code Example
    
    
        
    The code for Convert object to JSON string
import groovy.json.*
class Person {
    String name
}
def per = new Person( name: 'Alvin Alexander' )
println new JsonBuilder( per ).toPrettyString()
/*
Output:
{
    "name": "Alvin Alexander"
}
*/
    
    
        Code by IncludeHelp,
        on August 8, 2022 16:36
        
    
package lecoincoin4
import groovy.json.*
  class Annonce {
    String title
    String description
    Float price
    Boolean isActive = Boolean.FALSE
    Date dateCreated
    Date lastUpdated
    List illustrations
    static constraints = {
      title blank: false,
      maxSize: 50,
      nullable: false
      description blank: false,
      nullable: false
      price min: 0 F,
      nullable: false
      isActive nullable: false
    }
    static mapping = {
      description type: 'text'
    }
    static hasMany = [illustrations: Illustration]
    static belongsTo = [author: User]
  }
    
    
        Code by Pweiss,
        on December 20, 2022 21:50
        
    
import groovy.json.JsonBuilder
class Person {
    String name
    String address
}
def o = new Person( name: 'John Doe', address: 'Texas' )
println new JsonBuilder( o ).toPrettyString()
    
    
        Code by SS,
        on January 4, 2023, 14:37