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

peak
  • 105,803
  • 17
  • 152
  • 177
Safney
  • 31
  • 6
  • 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](https://stackoverflow.com/a/59036879/45375) to the linked duplicate for details. – mklement0 Oct 09 '21 at 14:54

1 Answers1

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)'
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • 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. – Safney Oct 09 '21 at 17:41
  • tried the below: `select(.\"hostname-label\"==\"$host_nme\")` it is not failing but no data is displayed. – Safney Oct 09 '21 at 17:53