0

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",
4
  • Please include a mcve. It looks like you have some garbage at the beginning Commented Sep 30, 2019 at 7:12
  • updated the question. Those are the only step. one is var=curl output and then echo $var Commented Sep 30, 2019 at 7:16
  • 1
    echo "$var" | ... Commented Sep 30, 2019 at 7:49
  • @JeffMercado I upvoted your comment but it actually doesn't change anything for jq since the stripped whitespaces are insignificant in JSON. It's also weird that when OP does echo $var without quotes the output seems to have linefeeds. Commented Sep 30, 2019 at 9:12

2 Answers 2

1

Your stdout of $var might not in a correct json format, let validate your $var at first (https://jsonlint.com/)

Then retry with following:

echo $var | jq '.result[].description'
Sign up to request clarification or add additional context in comments.

Comments

0

Using jq, you basically have two choices:

echo $var | jq '.. | .description?'

or if you want to be more specific:

echo $var | jq '.result[] | .description?'

But there are other variations ...

1 Comment

getting the following error echo $var | jq '.result[] | .description?' jq: error (at <stdin>:1): Cannot index string with string "result"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.