Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break jq query string into lines

Tags:

json

linux

bash

jq

How can i break jq string into lines, this is for long lines, when i put "\" query breaks.

vpcExists=$(aws ec2 describe-vpcs --profile $profile | jq -r --arg vpcId "$vpcId" '.[][] | \
 select(.VpcId == $vpcId) \
| .["State"]' \
)
like image 216
Omar Alvarado Avatar asked Feb 09 '18 19:02

Omar Alvarado


Video Answer


1 Answers

jq is fine with literal line breaks, so just add linefeeds anywhere without trying to escape them:

vpcExists=$(aws ec2 describe-vpcs --profile $profile |
    jq -r --arg vpcId "$vpcId" '
   .[][] 
     | select(.VpcId == $vpcId)
     | .["State"]' 
)

Here's a MCVE:

jq -r --arg vpcId "someId" '
   .[][] 
     | select(.VpcId == $vpcId)
     | .["State"]'  << 'EOF'


{ "Vpcs": [ {
            "VpcId": "someId",
            "InstanceTenancy": "default",
            "State": "available",
            "IsDefault": false
        } ] }
EOF
like image 114
that other guy Avatar answered Oct 19 '22 19:10

that other guy