Using JSON to configure your scripts

The target of this recipe is to show how to use JSON as a format for configuring a Groovy application.

The requirements for a configuration file format are:

  • Simple
  • Human-readable
  • Cross-platform
  • Multi-language support
  • Unicode support

JSON fully satisfies the above requirements, thus making a great candidate for configuring an application.

Getting ready

For this recipe, we work on a fictional application that maintains a connection to multiple databases. Each database has a number of parameters to configure such as host, port, credentials, and connection pool initial size.

The following is a sample JSON based configuration file for our application:

{ "configuration":{ "database":[ { "name":"database1", "host":"10.20.30.40", ...

Get Groovy 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.