Filtering issues with JSON

Viewed 39

I can't use environment variables inside a string in jmespath. Using a wildcard could have also worked but it didn't work in my case either. Any leads on this issue would be appreciated thanks!

aws logs describe-log-groups | jq -r --arg CLUSTER_NAME "$EKS_CLUSTER_NAME" '.logGroups[] | select(.logGroupName == '/aws/eks/$CLUSTER_NAME/cluster')'

Output:

jq: error: syntax error, unexpected '/' (Unix shell quoting issues?) at <top-level>, line 1:
.logGroups[] | select(.logGroupName == /aws/eks//cluster)
jq: 1 compile error
1 Answers

You have two issues:

  • Your inner jq quotes conflict with the outer shell quotes. Replace the inner ones with double quotes ", which is the way to use string literals in jq.
  • To include a variable's value inside a string in jq you would either add up the parts (e.g. using + as in ".." + $var + ".."), or use string interpolation "..\($var).."

Try

jq -r --arg CLUSTER_NAME "$EKS_CLUSTER_NAME" '
  .logGroups[] | select(.logGroupName == "/aws/eks/\($CLUSTER_NAME)/cluster")
'
Related