Every format with its parser
In this Teratip form our Teaclouds experts scenario, we are in a GNU/Linux console environment, which could be inside a Jenkins file for example, and we need a JSON object. A very common situation actually.
Along many years we have seen things like:
With also very complex to read sed structures to fill placeholders.
Or even run a sh script in a jenkinsfile that calls python code to be able to use the python library to create the JSON and later send it back to sh which sends it back to Jenkins. Crazy, right?
But we have jq. In the beginning, you might think it is difficult to learn but if you type man jq, you will find many examples describing multiple and very useful situations.
Let’s take a look at the following example. In this case we will use jq to create a JSON object filling up some placeholders and make a Slack API call request with it.
The -n parameter in jq allows you to create a new object from scratch without taking any input to parse.
The — arg parameter creates a variable named as the first argument inside the structure and fills it with whatever you write as the second argument. As you can see, placeholders are OK. No need to escape anything here.
The last parameter is the JSON template. Pay special attention to where the dollar sign needs to be placed. Could be mischievous at first sight.
Since all of this runs in a $() subshell, the output will be stored in the data variable.
Now you can use it with your curl by just calling — data “$data”
Time to play with jq now! Let us know how you used to create your JSON objects in the past, before this Teratip showed you how to do it properly.
Teracloud is an AWS Advanced Consulting Partner specialized in Cloud Computing (AWS, Azure, GCP) best practices. We help SaaS of different industries around the world scale their business safely.
Contact our team at firstname.lastname@example.org for an assessment. Let us handle migration, automation, deployment, performance, cost optimization and machine learning to make your business grow.
We make the cloud easy for you!