How to stop Jenkins CI build by extract a value from a JSON endpoint in bash using Curl

Curl the URL to get the JSON response and pipe it to the python tool. The python code will extract the value mapped to the given key (number in this case).


machine='dbelab04'
machineport=8080
jobname='mkat_fpga_tests_4k'
buildnumber=$(curl --silent -X POST http://$machine:$machineport/job/$jobname/lastBuild/api/json?tree=number | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["'number'"]';)
curl -X POST http://$machine:$machineport/job/$jobname/$number/stop

Note: the above command actually works