I put the json file into grails-app/conf/resources
and edited Bootstrap.groovy
.
At the top of the file, I injected the grails application:
class BootStrap {
def grailsApplication
...
And later on I loaded the data:
if (!Cars.count()) {
// add cars
def filePath = "resources/carsData.json"
def text = grailsApplication.getParentContext().getResource("classpath:$filePath").getInputStream().getText()
def json = JSON.parse(text)
for (carData in json) {
def c = new Cars(
name: carData ["name"],
year: carData ["year"]
).save(failOnError: true);
}
}