# JQ ## Piping into jq You can `cat` or `bat` a file and pipe it into `jq`. You can also take a command that returns json and pipe it into `jq`. ## Returning data without " To return data without `jq` wrapping results in `"` use the `-r` flag. `jq -r` ## Filtering ### Get values from a key Running `aws --profile admin cloudformation describe-stack-resources --stack-name strapi-vpc | jq` returns: ```json { "StackResources": [ { "StackName": "strapi-vpc", "StackId": "arn:aws:cloudformation:eu-west-1:745437999005:stack/strapi-vpc/a9e41430-8afc-11ea-bdaa-0a736ea8438a", "LogicalResourceId": "InternetGateway", "PhysicalResourceId": "igw-0e059db8e0795ac32", "ResourceType": "AWS::EC2::InternetGateway", "Timestamp": "2020-04-30T16:07:42.434Z", "ResourceStatus": "CREATE_COMPLETE", "DriftInformation": { "StackResourceDriftStatus": "NOT_CHECKED" } }, { "StackName": "strapi-vpc", "StackId": "arn:aws:cloudformation:eu-west-1:745437999005:stack/strapi-vpc/a9e41430-8afc-11ea-bdaa-0a736ea8438a", "LogicalResourceId": "InternetGatewayAttachment", "PhysicalResourceId": "strap-Inter-1413K0IDR1L3N", "ResourceType": "AWS::EC2::VPCGatewayAttachment", "Timestamp": "2020-04-30T16:08:00.147Z", "ResourceStatus": "CREATE_COMPLETE", "DriftInformation": { "StackResourceDriftStatus": "NOT_CHECKED" } }, ``` We can then use `jq`'s filtering to return values. We have a key of `StackResources` which contains a list: `.StackResources[]` We can then pass in the key we want `.StackResources[].PhysicalResourceId` `aws --profile admin cloudformation describe-stack-resources --stack-name strapi-vpc | jq -r '.StackResources[].PhysicalResourceId'` which gives: ```json "igw-0e059db8e0795ac32" "strap-Inter-1413K0IDR1L3N" "strap-Publi-1TS82BV8W4UFD" "rtb-0cf8d05f71a30ef03" "subnet-051fe56dc37d8396d" "rtbassoc-0f7ae2fbdfe6bf2a5" "subnet-0ea9f2f165a57be27" "rtbassoc-00a67937c3778e273" "subnet-09b28d722f41b2dde" "rtbassoc-0a0a6bd0f8ff641df" "vpc-029d232726cbf591d" ``` `aws --profile admin cloudformation describe-stack-resources --stack-name strapi-vpc | jq -r '.StackResources[] | .ResourceType + ": " + .PhysicalResourceId'`