0

How do a query a variable which has json value using jq. Below the command returs a json assigns to activity variable. But from activity variable not sure how to query using jq?

activity=`aws autoscaling describe-scaling-activities --profile mfa --auto-scaling-group-name cb-citation-needed --max-items 1`

When I try below it the variable activityCause is empty.

activityCause=$activity| jq -r '.Activities[].Cause'

or

activityCause=`$activity| jq -r '.Activities[].Cause'` i get error for this one

However when I try to query in one single command it works

activityCause=`aws autoscaling describe-scaling-activities --profile mfa --auto-scaling-group-name cb-citation-needed --max-items 1 | jq -r '.Activities[].Cause'`
oguz ismail
  • 1
  • 16
  • 47
  • 69
kumar
  • 8,207
  • 20
  • 85
  • 176

1 Answers1

1

The first command assigns the JSON to the activity variable.

The second command, need's to be altert to 'feed' the json through the pipe since in activityCause=$activity $activity is interpreted as a command, and not the JSON;

  1. Use bash Here

    activityCause=$(jq -r '.Activities[].Cause' <<< "$activity")
    
  2. Use oldschool echo:

    activityCause=$(echo "$activity" | jq -r '.Activities[].Cause')
    
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Tip: `echo` can mess up some strings, and works differently on different systems. `printf '%s' "$activity"` is more reliable. – ikegami Dec 23 '20 at 08:56