Search code examples
powershelljqoracle-cloud-infrastructure

Jq - Syntax error when ran on windows powershell however on bash terminal it was successful


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


Solution

  • 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)'