I've stored curl json output to a variable. And I want to retrieve only the description and store it in another variable.
I tried jq and grep but doesn't work.
var=`curl -i -X POST -H 'Content-Type: application/json' -d '
{
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"filter": {"value": "1"},
"sortfield": "lastchange",
"limit": 20
},
"auth": "authstring",
"id": 1
}' http://127.0.0.1/zabbix/api_jsonrpc.php`
echo $var
{
"jsonrpc":"2.0",
"result":[
{
"triggerid":"17169",
"expression":"{19444}=1",
"description":"Zabbix agent on {HOST.NAME} is unreachable for 5 minutes",
"url":"",
"status":"0",
"value":"1",
"priority":"3",
"lastchange":"1569589239",
"comments":"",
"error":"",
"templateid":"13437",
"type":"0",
"state":"0",
"flags":"0",
"recovery_mode":"0",
"recovery_expression":"",
"correlation_mode":"0",
"correlation_tag":"",
"manual_close":"0",
"details":""
},
{
"triggerid":"18123",
"expression":"{20525}=1",
"description":"Zabbix agent on {HOST.NAME} is unreachable for 5 minutes",
"url":"",
"status":"0",
"value":"1",
"priority":"3",
"lastchange":"1569590703",
"comments":"",
"error":"",
"templateid":"13437",
"type":"0",
"state":"0",
"flags":"0",
"recovery_mode":"0",
"recovery_expression":"",
"correlation_mode":"0",
"correlation_tag":"",
"manual_close":"0",
"details":""
}
],
"id":1
}
echo $var | jq -r '.description'
parse error: Invalid numeric literal at line 1, column 9
Any idea what's that error means? Also how could I achieve this with grep?
Figured out how to do this with grep. So now I only need to figure out what the jq error means and how to correct it.
echo $var | grep -Po '"description":.*?[^\\]",'
"description":"Zabbix agent on {HOST.NAME} is unreachable for 5 minutes",
"description":"Zabbix agent on {HOST.NAME} is unreachable for 5 minutes",
curl outputand then echo $varecho "$var" | ...jqsince the stripped whitespaces are insignificant in JSON. It's also weird that when OP doesecho $varwithout quotes the output seems to have linefeeds.