Table of Contents
In this article, we will look into the steps to Install Jsonnet on Ubuntu 20.04 LTS. You might be hearing 'Jsonnet' word a lot and thinking if it's a utility, language or a file type ? Well, its actually all of them. You can get Jsonnet open source utility and generate your JSON files by using Jsonnet data templating language. With JSON becoming more and more popular among developers for sending and receiving the payload, it is more than required to have a language that can be easily used to generate JSON files directly. This job has now become much easier with the onset of Jsonnet data templating language. More on Jsonnet.
What is Jsonnet
Jsonnet is an open source data templating language from Google which allows you to define JSON data in a specific template.
How to Install Jsonnet on Ubuntu 20.04 LTS
Also Read: 6 Easy Steps to Install Helm Kubernetes Package Manager on Linux
Step 1: Prerequisites
a) You should have a running Ubuntu Server.
b) You should have access to run privileged commands.
c) You should have apt
or apt-get
utility installed in your System.
Step 2: Update Your Server
First you need to update your Ubuntu Server by using sudo apt update
or by just using apt update
(for root user) command as shown below.
root@localhost:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:4 https://packages.microsoft.com/repos/edge stable InRelease [7,342 B]
Ign:5 http://ppa.launchpad.net/oguzhaninan/stacer/ubuntu focal InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [527 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,175 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [254 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [283 kB]
Get:11 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease [17.5 kB
Step 3: Install Jsonnet
Next step is to download and install Jsonnet utility from Ubuntu Repo using sudo apt install jsonnet
or by just using apt install jsonnet
(for root user) command as shown below.
root@localhost:~# apt install jsonnet Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: fonts-inter libdouble-conversion3 libpcre2-16-0 libqt5charts5 libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5 libqt5widgets5 libxcb-xinerama0 libxcb-xinput0 qt5-gtk-platformtheme qttranslations5-l10n Use 'apt autoremove' to remove them. The following NEW packages will be installed: jsonnet 0 upgraded, 1 newly installed, 0 to remove and 160 not upgraded. Need to get 378 kB of archives. After this operation, 1,964 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 jsonnet amd64 0.15.0+ds-1build1 [378 kB] Fetched 378 kB in 1s (442 kB/s) Selecting previously unselected package jsonnet. (Reading database ... 189065 files and directories currently installed.) Preparing to unpack .../jsonnet_0.15.0+ds-1build1_amd64.deb ... Unpacking jsonnet (0.15.0+ds-1build1) ... Setting up jsonnet (0.15.0+ds-1build1) ...
Step 4: Check Jsonnet version
You can use jsonnet -v
command to check the installed version of jsonnet utility as shown below. As you can see, current version is 0.15.0
root@localhost:~# jsonnet -v Jsonnet commandline interpreter v0.15.0
Step 5: Create JSON File
Now that you have jsonnet utility installed, let's do a quick example to understand how it can be used to generate JSON file. We will see an example from GitHub to demonstrate the usage of jsonnet utility. In the below jsonnet example, you will see different operations liking mixing objects together, string formatting, bitwise operation etc. being performed.
root@localhost:~# cat Demo.jsonnet
{
concat_array: [1, 2, 3] + [4],
concat_string: '123' + 4,
equality1: 1 == '1',
equality2: [{}, { x: 3 - 1 }]
== [{}, { x: 2 }],
ex1: 1 + 2 * 3 / (4 + 5),
// Bitwise operations first cast to int.
ex2: self.ex1 | 3,
// Modulo operator.
ex3: self.ex1 % 2,
// Boolean logic
ex4: (4 > 3) && (1 <= 3) || false,
// Mixing objects together
obj: { a: 1, b: 2 } + { b: 3, c: 4 },
// Test if a field is in an object
obj_member: 'foo' in { foo: 1 },
// String formatting
str1: 'The value of self.ex2 is '
+ self.ex2 + '.',
str2: 'The value of self.ex2 is %g.'
% self.ex2,
str3: 'ex1=%0.2f, ex2=%0.2f'
% [self.ex1, self.ex2],
// By passing self, we allow ex1 and ex2 to
// be extracted internally.
str4: 'ex1=%(ex1)0.2f, ex2=%(ex2)0.2f'
% self,
// Do textual templating of entire files:
str5: |||
ex1=%(ex1)0.2f
ex2=%(ex2)0.2f
||| % self,
}
To compile above jsonnet file, you can use jsonnet -o Demo.json Demo.jsonnet
command as shown below.
root@localhost:~# jsonnet -o Demo.json Demo.jsonnet
If you now check the generated Demo.json
file using cat Demo.json
then it should look like below.
root@localhost:~# cat Demo.json
{
"concat_array": [
1,
2,
3,
4
],
"concat_string": "1234",
"equality1": false,
"equality2": true,
"ex1": 1.6666666666666665,
"ex2": 3,
"ex3": 1.6666666666666665,
"ex4": true,
"obj": {
"a": 1,
"b": 3,
"c": 4
},
"obj_member": true,
"str1": "The value of self.ex2 is 3.",
"str2": "The value of self.ex2 is 3.",
"str3": "ex1=1.67, ex2=3.00",
"str4": "ex1=1.67, ex2=3.00",
"str5": "ex1=1.67\nex2=3.00\n"
}
Step 6: Uninstall Jsonnet
If you are done using Jsonnet utility then you can simply uninstall it by using sudo apt remove jsonnet
or by just using apt remove jsonnet
(for root user) command as shown below.
root@localhost:~# apt remove jsonnet Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: fonts-inter libdouble-conversion3 libpcre2-16-0 libqt5charts5 libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5 libqt5widgets5 libxcb-xinerama0 libxcb-xinput0 qt5-gtk-platformtheme qttranslations5-l10n Use 'apt autoremove' to remove them. The following packages will be REMOVED: jsonnet 0 upgraded, 0 newly installed, 1 to remove and 243 not upgraded. After this operation, 1,964 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 189133 files and directories currently installed.) Removing jsonnet (0.15.0+ds-1build1) ...