Today I had some data coming into our event driven landscape, so I needed to know when my data was processed. As it constituted the processing of 400.000+ records (and I had more things to do), I needed a small script that kept an eye on my JSON API endpoint to see if the values are changed.

CURL+JQ to the rescue

We’ll perform the call to the web API with cURL and process the JSON-result with jq. I ended up with the following code:

printf "\nPolling '$url' every $interval_in_seconds seconds, until '$result'\n"
while true; 
	x=$(curl $url -s | jq $path); 
	printf "\r$(date +%H:%M:%S): $x";
	if [[ "$x" == "$result" ]]; then 
	sleep $interval_in_seconds; 

Note: I’m using a weather API as an example and in this case, I’m checking the air pressure in Amsterdam. It looks something like this:

Querying a weather API.
Querying a weather API.

How about something smaller?

It is technically a one-liner with many semi-columns and abbreviated variable-names:

u="";p=".consolidated_weather[0].air_pressure";r="1024";s=2;printf "\nPolling '$u' every $s seconds, until '$r'\n";while true;do x=$(curl $u -s | jq $p);printf "\r$(date +%H:%M:%S): $x"; if [[ "$x" == "$r" ]]; then break;fi;sleep $s;done