The issue with your jq command is related to string interpolation syntax and quote handling in the shell. Here’s why it fails and how to fix it:
jqIn jq, you must use double quotes for string literals, and you can interpolate values using \( ... ). However, your command mixes shell and jq quoting, which causes the error.
Your command is:
bashecho '[{"a": true}]' | jq '.[] | "\(if .a then \"A\" else \"B\" end) \(.a)"'
\"A\" and \"B\" as escaped quotes, but jq expects them to be part of the string interpolation.if statement inside the string is not properly closed or formatted for jq.To fix this, use raw string literals (@text or @json) or ensure proper escaping and quoting for jq:
@text for simple outputbashecho '[{"a": true}]' | jq '.[] | "\(.a as $a | if $a then "A" else "B" end) \(.a)"'
@json for JSON outputbashecho '[{"a": true}]' | jq '.[] | "\(.a as $a | if $a then "A" else "B" end) \(.a)"'
bashecho '[{"a": true}]' | jq '.[] | .a as $a | "\(if $a then "A" else "B" end) \($a)"'
bashecho '[{"a": true}]' | jq '.[] | "\(.a ? "A" : "B") \(.a)"'
? :) for concise logic.json"A true"
bashecho '[{"a": true}]' | jq '.[] | "\(.a ? "A" : "B") \(.a)"'
Output:
text"A true"
Note:
Always use double quotes for jq strings, and escape inner quotes with \ only if necessary. The ternary operator (? :) is the most idiomatic way to handle simple conditionals in jq.