Variable containing double quotes

I have a variable that contains JSON and receive the below error -

Dialplan: Set(PJSIP_HEADER(add,HeaderName)=${MASTER_CHANNEL(HeaderName)})

ast_expr2.fl:470 ast_yyerror: ast_yyerror(): syntax error: syntax error, unexpected ‘’, expecting $end;

"{"key1":"val1","key2":"val2"}" != ""

Is there anyway to deal with this? The JSON contents needs to remain unchanged.

${MASTER_CHANNEL(HeaderName)} contains {“key1”:“val1”,“key2”:“val2”}

Asterisk 18.14.0

why you need put this complicate string to header ?

Either you haven’t correctly copied this, or your quote marks are not Quotation Marks, but rather a combination of Left Double Quotation Mark and RIght Double Quotation Mark.

Also the error message is reporting a null string, not a quotation mark.

Have you actually used UTF-8, as ISO-8859-1 may well created malformed UTF-8 code, which would cause a syntax error, in the actual SIP header.

The above uncertainties also mean that I can’t trust the position of the carat.

I don’t think the diaplan parser would invoke a yacc grammar, here, so I think we need to see where the value came from. I think the expression parser only gets invoked for $[…].

Carrot position has been fixed.

This error does not occur in Asterisk 13.

Call to GotoIf:
GotoIf($["${MASTER_CHANNEL(HeaderName)}" != ""]?x:y)

Log result: 
GotoIf("PJSIP/xxxxxxxxxxxxx", ""{"?x:y") in new stack

The log result expected is:
GotoIf("PJSIP/xxxxxxxxxxxxx", "1?x:y") in new stack

I’d try the EMPTY function. I don’t think you will need quotes and they won’t be in an expression context.

Have you tried JSON_DECODE() since that looks like a json object?

He says he wants to propagate it unchanged.

I only need to detect if the header is present and not empty.

The quotes in the json object are being processed by Asterisk. If you are just looking to see if the header has content and aren’t validating the content then wouldn’t this work?

GotoIf($["${LEN( ${MASTER_CHANNEL(HeaderName)} )}" > 0]?x:y)

Your code is attempting to test whether it is empty. I don’t think there is a way of testing for present but empty.

(The EMPTY function is cleaner than the length test, as one doesn’t need to use expression context ( $[....] ) at all.

Are you referring to ISNULL? I dont see an EMPTY function?

Sorry, I meant EXISTS()

Same error because EXISTS evaluates to “{” instead of 1


Functions in read contexts have to be de-referenced using ${…}. You don’t need to invoke an expression context.

One concern I would have is that code to pass through complex strings may be vulnerable to code injection attacks.

Definitely a valid concern, in this case there are protections for that.

When I tested EXISTS I used in expression context within GotoIf, is there a better way to call this?

Yes, checking header with LEN works as it doesn’t evaluate the contents.

Thanks for the idea!

1 Like


Ok, that worked. Thanks for the input!