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:

url="https://www.metaweather.com/api/location/44418/"
interval_in_seconds=2
result="1024"
path=".consolidated_weather[0].air_pressure"
printf "\nPolling '$url' every $interval_in_seconds seconds, until '$result'\n"
while true; 
do 
	x=$(curl $url -s | jq $path); 
	printf "\r$(date +%H:%M:%S): $x";
	if [[ "$x" == "$result" ]]; then 
		break; 
	fi; 
	sleep $interval_in_seconds; 
done

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="https://www.metaweather.com/api/location/44418/";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