Trouble parsing a Json response in dialplan with jq

I am running into a issue trying to parse a RestAPI call in a dialplan which I want to use to block calls. if i do that my result is always blank.

exten => s,n,NoOp('This is called before we call block section')
exten => s,n,NoOp(${CALLER_NUMBER})
exten => s,n,NoOp(${CALLEE_NUMBER})
exten => s,n,Set(result=${SHELL(echo '${CURL_RESULT}' | jq '.status')})
exten => s,n,NoOp(${result})

exten => s,n,Hangup

if i test from my server via below code

curl -v | jq '.status'

i get the desired response and the value i am looking for. What am i missing here or is there a better way to parse values into the dialplan from a RestAPI call.

I am running on Asterisk 13.32.0 , CentOs 6 and jq-1.5

Hope someone can point me in right direction

Combine both lines to something like:

exten => s,n,Set(result=${SHELL(curl -v${CALLEE_NUMBER}/${CALLER_NUMBER} | jq '.status')})

This doesn’t work either and wouldn’t be the ideal solution as i want to be able to assign multiple call variables based on the curl response.
But i tried use …

exten => s,n,Set(result=${SHELL(curl${CALLEE_NUMBER}/${CALLER_NUMBER} | jq '.status')})
exten => s,n,NoOp('Result: ' ${result})

with no luck…
Here is the output of debug in my dial plan of these 2 lines

and here is what the curl returns…

  "success": true,
  "callingnbr": "222",
  "callednbr": "12345678901",
  "status": 0

I would suggest use AGI and set SET VARIABLE command

Asterisk channel variables really don’t like newlines, as I recall.

there shouldn’t be a new line when you use jq as it should read and return only the value of the key specified. I am suprised that json is not in higher demand in the astrisk community.
So how would i go about troubleshooting why it doesn’t not return anything. We know if i uonly run the curl i get data. when i use the jq i get nothing. if i use jq from my commandline on server i get what i expect

Treat it as an expression like …result=$[SHELL(curl…status’])})

I have the below working:

exten => s,1,Noop(Getting country code...)
exten => s,n,Set(country=${SHELL(curl -s | jq .'country'):1:-2})
exten => s,n,Noop(Country code is: ${country})
exten => s,n,Hangup()

And the log…

    -- Executing [s@get-country-code:1] NoOp("SIP/101-000001cc", "Getting country name...") in new stack
    -- Executing [s@get-country-code:2] Set("SIP/101-000001cc", "country=US") in new stack
    -- Executing [s@get-country-code:3] NoOp("SIP/101-000001cc", "Country code is: US") in new stack
    -- Executing [s@get-country-code:4] Hangup("SIP/101-000001cc", "") in new stack

Thanks, i am missing something which causes this to fail. I used your sample code in my dial plan

exten => s,n,Set(country=${SHELL(curl -s | jq .'country'):1:-2})
exten => s,n,Noop(Country code is: ${country})

and got the following result

-- Executing [s@macro-tl-custom-userexten:78] Set("SIP/2WAY-00000060", "country=") in new stack
-- Executing [s@macro-tl-custom-userexten:79] NoOp("SIP/2WAY-00000060", "Country code is: ") in new stack

Just to test i run it with 

exten => s,n,Set(country=${SHELL(curl -s})  i get the whole json doc so we know curl is not the issue but it seems to be the issue running jq from within dialplan

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.