0

I was trying to parse an OCI JSON output using JQ and getting syntax error in Windows powershell however the same command is working fine in Bash terminal. Pleae let me know if i miss anything.

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxxx  --profile myprof |C:\myfiles\jq-win64.exe ".data[]|select(."hostname-label"=="test1" )"

jq: error: syntax error, unexpected $end, expecting ';' or ')' (Windows cmd shell quoting issues?) at <top-level>, line 1: .data[]|select(. jq: 1 compile error PS C:myfiles>

Tried with the JQ statement in single quotes, its giving below syntax error:

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe '.data[]|select(."hostname-label"=="test1" )'

jq: error: syntax error, unexpected ==, expecting '$' (Windows cmd shell quoting issues?) at <top-level>, line 1: .data[]|select(.hostname-label==test1 ) jq: 1 compile error PS C:\MYDATA\myfiles>

I tried the same in jplay.org and it was working fine there as well. Please let me know how to fix this in Windows.

Thanks

1
  • 1
    The sad reality as of PowerShell 7.1 is that an extra, manual layer of \ -escaping of embedded " characters is required in arguments passed to external programs. This may get fixed in 7.2, which may require opt-in. See this answer to the linked duplicate for details. Commented Oct 9, 2021 at 14:54

1 Answer 1

2

Double quotes inside single quotes need to be escaped :

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe '.data[]|select(.\"hostname-label\"==\"test1\")'

If you have variable, pass it as argument :

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe --arg host_name $host_name '.data[]|select(.\"hostname-label\"==$host_name)'
Sign up to request clarification or add additional context in comments.

2 Comments

how to get proper output in case of variable in my select condition? select(.\"hostname-label\"==\"$host_nme\").id conidtion is failing and i am getting all the data instead of the host which i am trying to filter.
tried the below: select(.\"hostname-label\"==\"$host_nme\") it is not failing but no data is displayed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.