Every format with its parser

Teracloud
2 min readOct 10, 2021

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:

To copy and paste: echo ‘{“channel”:”main”,”text”:”Hello %%USER%%”}’ > message.json
Every format with its parser

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.

To copy and paste: data=$( jq -n \ — arg SLACK_CHANNEL “${SLACK_CHANNEL}” \ — arg text “Hello ${USER}.” \ ‘{channel:$SLACK_CHANNEL,text:$text}’ ) curl -X POST \ -H “Authorization: Bearer $SLACK_AUTHORIZATION” \ -H ‘Content-type: application/json; charset=utf-8’ \ — data “$data” \ https://slack.com/api/chat.postMessage

Explanation:

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 info@teracloud.io 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!

--

--

Teracloud

Our thoughts on everything Cloud, Expanding knowledge on our expertise and services