Grails parse JSON into domain class

10,659

Using the built-in Grails JSON converter makes this easier

import grails.converters.JSON

class BookController {
   def save = {
      def book = new Book(JSON.parse(yourJson))
      book.save(flush:true)
   }
}

In the code what's happening (we're parsing a JSON object and setting the properties on the Book entity and saving

Share:
10,659
user903772
Author by

user903772

Updated on July 20, 2022

Comments

  • user903772
    user903772 almost 2 years

    Hi Say that I have a domain class

    class Book{
    static hasOne=[author:Author]
    long id
    String name
    
    }
    
    class Author {
    static hasMany=[books:Book]
    long id
    String name
    }
    

    I have a json object sent in. Can I jus do a new Book(Json) and not manually set the property?