If you are a cloud system administrator, security analyst, or data scientist, you already use this marvelous command-line tool. If you don't, it's time to learn something new and improve your scripting capabilities.
If you think that JQ stands for JSON Query, you guess it right. It is a command-line JSON query tool that extracts and transforms data from JSON sources. We live at the Big Data More down, when more and more systems and services produce and consume JSON formatted information. You can name any shred of information that comes to your mind: messages, metadata, log entries, anything that other systems may consume.
Unfortunately, I cant consume JSON directly, and I prefer formatted tables and well-formatted emails. And from time to time, you need to use one particular value from the three-page output. So without any further ado, let's go through a few examples.
Most likely, you don't have the jq utility installed on your VM, so install it or and your system administrator to install it for you:
Since I use my Always Free OCI instance, the most natural JSO source would be a compute instance metadata, and I want to keep it small, so let's start with VNICs.
- Make your JSON output much more readable.
The query result shows a single-item list, and it matches the input. Because the default query is '.' (period), which in human words means "select the current object."
2. Select the first object in the list. Remember, we start the index with 0.
3. Let's query some key values, for example, the CIDR block for this instance. We do it the same way ad the first object
curl -s -H "Authorization: Bearer Oracle" \ http://169.254.169.254/opc/v2/vnics | \ jq '..subnetCidrBlock'
4. You can request multiple keys and use them to produce new objects
5. Let's get the same result but more conventional, with the join function.
Now you get the idea of how powerful this tool is. To unlock its full potential, check some additional resources: