New! 10x more awesome ways to write JSON configuration using Jsonnet.
In my last two projects, I’ve seen developers writing different configurations in JSON with a lot of code repeated and a horrible design to make changes. Jsonnet is the most simple, reliable and side-effect free language; I could find to make my team’s life easier. In this article, I’ve explained some of the challenges one face to write configurations in plain JSON and how Jsonnet can help.
JSON format, intended initially to replace XML for data-interchange, is widely used today to provide public data using APIs and web services as well as to write various configuration files.
JSON can store information in an organised, human-readable and machine-readable format, but the emerging trend of writing configurations in JSON is something that a lot of people don’t like.
Before I reveal the awesome way to tackle the problem, Let’s quickly see why people believe it is a bad idea to write configuration in plain JSON format.
No comments allowed
Weird, right! You’re not allowed to add any comments in the JSON file, which sometimes encourages people to write ugly code like below.
Not so good way to put comments in JSON files.
In most of the configurations, I’ve experienced there are sections/values which are used multiple times in the same or multiple JSON configuration files. I’m sure you would have With the plain JSON format, there is no way to avoid duplication.
Consider below an example of an environment configuration file, where the IP address “10.0.0.120” is used three times in a single configuration file.
Imagine this same IP address is used multiple times in other configuration files. How would one track if we need to change this IP in future or how would one remove the duplicate entries without changing the schema?
You might think why can’t just put a placeholder and use a programming language to replace placeholders with the actual values. Well, a lot of people do that but I’ll explain later in this article why putting placeholders is not a good idea.
With the plain JSON, you can’t do cool geeky stuff like variables, conditional statements, loops, or import other JSON files etc.
To output, the data from a REST API or web service, programmability in JSON might not look useful. But when writing configurations, programmability will add a lot of value and save a lot of extra efforts to make the code clean and re-usable.
Consider below an example of a monitoring configuration file to monitor four services in production, dev and test environments.
Do you imagine it is easy to write and read the above configuration?
Well! Maybe not too difficult. However, think of following different scenarios, where writing configuration in JSON might be complicated:
- We have fifty or even more different services to monitor in five or more environments.
- Some of the services exist in production but not in non-production environments.
- FQDN (fully-qualified-domain-names) for the services are dynamic and imported from other files.
Now that you understand the challenges people face in the real world to write configurations in JSON let me introduce the awesome language owned by Google called “Jsonnet” to solve all the above-listed problems and do a lot more than that.
So what is Jsonnet?
In a nutshell, Jsonnet is an object-oriented data templating language to generate the config data. It’s an extension of the JSON, and it helps to organise, simplify and unify JSON data by adding programming capabilities such as variables, conditional statements, loops, functions, imports etc. to JSON.
Jsonnet understands JSON means any valid JSON is a valid Jsonnet. Let’s see how Jsonnet helps us to write configuration in a better way.
Jsonnet support comments
Jsonnet supports multiple styles of comments as shown in below code.
Avoid duplication using variables
Jsonnet supports the following types of variables to avoid duplication.
1. Standard local variable.
2. A local variable next to JSON fields.
3. Hidden variables.
Jsonnet supports all sort of programming features like loops, conditional statements, functions, imports etc. There is even a standard library of Jsonnet functions available out of the box. Following is a fundamental example of loops to demonstrate the programmability.
All the examples above are very high level and don’t cover Jsonnet in depth. If you are interested in learning more about Jsonnet, check out the following articles:
- Beginner tutorial
- Jsonnet’s standard library
- Comparison with other languages
- Jsonnet Github source
- Jsonnet style guide by Databricks
I’ll be writing some advanced tutorials with real examples on Jsonnet soon, make sure you follow my profile to stay tuned.
If you like this post, show your love by giving 50 claps. This will encourage me to write more. :)