6

I am trying really hard to get the Windows shell working with jq and failing miserably.

I want this type of thing to work

echo '["a","b","c"]' | .\Downloads\jq.exe -r '{ "data": map({ "{#SNAME}": . })}'

But I get an error:

jq: error: syntax error, unexpected '.' (Windows cmd shell quoting issues?) at , line 1:

If i just do echo '["a","b","c"]' | .\Downloads\jq.exe -r '.' then it's happy but as soon i i add in other characters such as echo '["a","b","c"]' | .\Downloads\jq.exe -r '{.}' then it fails again with:

jq: error: syntax error, unexpected '.' (Windows cmd shell quoting issues?) at , line 1:

Does anyone know how to make Windows shell happy with jq for the above examples to work as expected ?

Using latest 1.6 build from jq website for these tests and confirmed the jq commands work using Linux and jqplay.org.

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Flo Woo
  • 949
  • 1
  • 12
  • 26

1 Answers1

10

You have three main options:

  1. (Easy) Put the JSON and jq program into separate files (or maybe, with care, into one file), and invoke jq accordingly.

  2. (Error-prone) Follow the quoting rules for the shell you're using.

  3. Some combination of the above.

The basic rule as I understand it is as follows: at a Windows cmd command-line prompt, in order to quote strings, you use double-quotes, and escape double-quotes within the string using backslashes.

For example:

C>ver
Microsoft Windows [Version 10.0.17134.590]

C>echo "hello \"world\"" | jq .
"hello \"world\""

C>jq -n "\"hello world\""
"hello world"

Your example

C>echo ["a","b","c"] | jq -c "{\"data\":map({\"{#SNAME}\":.})}"
{"data":[{"{#SNAME}":"a"},{"{#SNAME}":"b"},{"{#SNAME}":"c"}]}

Postscript

Except for the hash (#) and braces ({}) in the string, one can achieve the goal by avoiding spaces:

C>echo ["a","b","c"] | jq -c {"data":map({"SNAME":.})}
{"data":[{"SNAME":"a"},{"SNAME":"b"},{"SNAME":"c"}]}

Powershell

Again, except for the hash and braces, simple solutions are possible:

Using single-quoted strings:

 echo '["a", "b", "c"]' | jq -c '{"data": map( {"SNAME": . })}'
 {"data":[{"SNAME":"a"},{"SNAME":"b"},{"SNAME":"c"}]}

Using "" inside double-quoted strings:

echo '["a", "b", "c"]' | jq -c "{""data"": map( {""SNAME"": . })}"
{"data":[{"SNAME":"a"},{"SNAME":"b"},{"SNAME":"c"}]}

The PowerShell documentation that I've seen suggests backticks can be used to escape special characters within double-quoted strings, but YMMV.

Bonne chance!

peak
  • 105,803
  • 17
  • 152
  • 177
  • Thanks but i don't think the problem is input related.. `echo '["a","b","c"]' | .\Downloads\jq.exe -r '.'` works fine. – Flo Woo Feb 14 '19 at 06:08
  • 2
    thank you, this was a great help. I believe part of my frustration was that my testing was being done in a windows prompt that was powershell based.. not just plain old cmd shell. This is a world of pain. – Flo Woo Feb 14 '19 at 07:04
  • excellent recommendation, jq-win64.exe -f filter flags_for_travis.json put filters separately from json and all good ! – Tiberiu May 10 '22 at 21:30