You have three main options:
-
(Easy) Put the JSON and jq program into separate files (or maybe, with care, into one file), and invoke jq accordingly.
-
(Error-prone) Follow the quoting rules for the shell you’re using.
-
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!
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Closed
DBAranAce opened this issue
Jun 9, 2014
· 14 comments
Closed
jq — windows examples
#392
DBAranAce opened this issue
Jun 9, 2014
· 14 comments
Assignees
Comments
Hi,
I am trying to run jq on an output from aws cli command but it does not work. i tried to run a simple example but it’s not working as well. can you provide with some windows examples i can start play with?
Thanks
whatever i use i get:
error: syntax error, unexpected INVALID_CHARACTER, expecting $end
do you have a windows example which can take an object like
{
«Timestamp»: «2014-06-01T10:00:00Z»,
«Maximum»: 1,
«Unit»: «None»
}
and create something like
«2014-06-01T10:00:00Z,1,»None»
it creates each value in a new row for me.
Thanks
It’s all about the quoting:
C:\temp>jq -r ".Timestamp + \",\" + (.Maximum | tostring) + \",\" +
.Unit»
{
«Timestamp»: «2014-06-01T10:00:00Z»,
«Maximum»: 1,
«Unit»: «None»
}
2014-06-01T10:00:00Z,1,None
The cmd.exe shell is not like a Unix shell…
I’ve updated the manual in master; I’ll update the site later today.
Can we have a simple and complete example to pretty print a json file on Windows?
One really should not have to spend more than 5 minutes reading through the docs and trying to figure out how in the world to accomplish the simplest possible JSON task, pretty-printing a .json file, without getting
error: syntax error, unexpected INVALID_CHARACTER, expecting $end
at every attempt.
jq '.' < file.json
fails. type file.json | jq '.'
fails etc. Highly frustrating.
yeah, for simple tasks like that it’s easier to just use jsonlint (and grep)
@dandv The problem is the shell, not jq. The problem here is that the shell doesn’t handle single quotes like a Unix shell: it passes the single quotes to jq, and since single quotes are not a normal part of a jq program, you get an error. I think we could add some rules to the parser to produce an error message that might help the user understand.
Anyways, jq .
(no single quotes) works on Windows.
It looks like even in powershell in windows you have to backslash the doublequotes…sometimes. Maybe powershell should be mentioned in the faq?
echo '"Hello"' | jq '. + \" world\"' "Hello world"
Can’t get this to work (it works for powershell in osx at least):
jq -n '\"Hello world!\"' jq: error: syntax error, unexpected $end, expecting QQSTRING_TEXT or QQSTRING_INTERP_START or QQSTRING_END (Windows cmd shell quoting issues?) at <top-level>, line 1: "Hello jq: 1 compile error
@jszabo98 Windows doesn’t use single quotes, and since you’re escaping the double quotes, it’s splitting on the space. Try jq -n "\"Hello world!\""
(which works for me).
Powershell uses both single quotes and double quotes. But somehow the double quotes sometimes get lost with external programs, even within single quotes.
@jszabo98 oh, that’s because on Windows there’s no proper argument vector, and programs have to do their own command-line parsing.
@jszabo98 Windows doesn’t use single quotes, and since you’re escaping the double quotes, it’s splitting on the space. Try
jq -n "\"Hello world!\""
(which works for me).
Sadly this doesn’t work for me. Here is my code:
IF "%COMPANY_ID%"=="" curl -H "Authorization: Bearer %ACCESS_TOKEN%" -X GET https://api.company.com/vapid/companies | jq ".[] | select (.name=="\"My Company Name\"") | .[].id"
@nicowilliams do you have any ideas?
¿Podemos tener un ejemplo simple y completo para imprimir bastante un archivo json en Windows?
Uno realmente no debería tener que pasar más de 5 minutos leyendo los documentos y tratando de descubrir cómo en el mundo realizar la tarea JSON más simple posible, imprimir un archivo .json, sin obtener
error: error de sintaxis, INVALID_CHARACTER inesperado, esperando $end
en cada intento.
jq '.' < file.json
fallatype file.json | jq '.'
falla, etc. Altamente frustrante.
type file.json | jq
A mi eso me sirvió Gracias
What I am doing?
I have one JSON file as sonar-report.json. I want to iterate sonar-report.json in shell script, to read values of json.
To parse JSON file I am using jq https://stedolan.github.io/jq/
So Following code I was trying to execute in shell script
alias jq=./jq-win64.exe
for key in $(jq '.issues | keys | .[]' sonar-report.json); do
echo "$key"
line=$(jq -r ".issues[$key].line" sonar-report.json)
done
Problem
When i execute this, console give me error:
jq: error: syntax error, unexpected INVALID_CHARACTER (Windows cmd shell quoting issues?) at <top-level>, line 1:
If I update my above script, and add static index of array then script works fine
alias jq=./jq-win64.exe
for key in $(jq '.issues | keys | .[]' sonar-report.json); do
echo "$key"
line0=$(jq -r ".issues[0].line" sonar-report.json)
line1=$(jq -r ".issues[1].line" sonar-report.json)
done
so at the end what i want :
I want to iterate values and print in console like
alias jq=./jq-win64.exe
for key in $(jq '.issues | keys | .[]' sonar-report.json); do
line=$(jq -r ".issues[$key].line" sonar-report.json)
echo $line
done
so the output should be
15
This is my JSON file as sonar-report.json
{
"issues": [
{
"key": "016B7970D27939AEBD",
"component": "bits-and-bytes:src/main/java/com/catalystone/statusreview/handler/StatusReviewDecisionLedHandler.java",
"line": 15,
"startLine": 15,
"startOffset": 12,
"endLine": 15,
"endOffset": 14,
"message": "Use the \"equals\" method if value comparison was intended.",
"severity": "MAJOR",
"rule": "squid:S4973",
"status": "OPEN",
"isNew": true,
"creationDate": "2019-06-21T15:19:18+0530"
},
{
"key": "AWtqCc-jtovxS8PJjBiP",
"component": "bits-and-bytes:src/test/java/com/catalystone/statusreview/service/StatusReviewInitiationSerivceTest.java",
"message": "Fix failing unit tests on file \"src/test/java/com/catalystone/statusreview/service/StatusReviewInitiationSerivceTest.java\".",
"severity": "MAJOR",
"rule": "common-java:FailedUnitTests",
"status": "OPEN",
"isNew": false,
"creationDate": "2019-06-18T15:32:08+0530"
}
]
}
please help me, Thanks in advance
Solution
This looks to me like an instance of Windows/Unix line-ending incompatibility, indicated in jq bugs 92 (for Cygwin) and 1870 (for MSYS2).
Any of the workarounds indicated in those bug reports should work, but once the fix gets into the release binary (presumably v1.7), the simplest solution is to use the new -b
command-line option. (The option is available in recent jq preview builds; see the second bug report listed above):
for key in $(jq -b '.issues | keys | .[]' sonar-report.json); do
line=$(jq -rb ".issues[$key].line" sonar-report.json)
# I added quotes in the next line, because it's better style.
echo "$line"
done
Until the next version of jq
is available, or if you don’t want to upgrade for some reason, a good workaround is to just remove the CRs by piping the output of jq
through tr -d '\r'
:
for key in $(jq -'.issues | keys | .[]' sonar-report.json | tr -d '\r'); do
line=$(jq -r ".issues[$key].line" sonar-report.json | tr -d '\r')
echo "$line"
done
However, as pointed out in a comment by Cyrus, you probably don’t need to iterate line-by-line in a shell loop, which is incredibly inefficient since it leads to reparsing the entire JSON input many times. You can use jq itself to iterate, with the much simpler:
jq '.issues[].line' solar-response.json
which will parse the JSON file just once, and then produce each .line
value in the file. (You probably still want to use the -b
command-line option or other workaround, depending on what you intend to do with the output.)
Answered By — rici
У вас есть три основных варианта:
-
(Легко) Поместите программу JSON и jq в отдельные файлы (или, может быть, осторожно, в один файл) и соответственно вызовите jq.
-
(Подвержен ошибкам) Следуйте правилам цитирования используемой оболочки.
-
Некоторая комбинация вышеперечисленного.
Основное правило, как я понимаю, заключается в следующем: в Windows cmd
Подсказка командной строки, чтобы заключить в кавычки строки, вы используете двойные кавычки и экранируйте двойные кавычки в строке, используя обратную косую черту.
Например:
C>ver
Microsoft Windows [Version 10.0.17134.590]
C>echo "hello \"world\"" | jq .
"hello \"world\""
C>jq -n "\"hello world\""
"hello world"
Ваш пример
C>echo ["a","b","c"] | jq -c "{\"data\":map({\"{#SNAME}\":.})}"
{"data":[{"{#SNAME}":"a"},{"{#SNAME}":"b"},{"{#SNAME}":"c"}]}
постскриптум
Кроме хеша (#
) и брекеты ({}
) в строке можно достичь цели, избегая пробелов:
C>echo ["a","b","c"] | jq -c {"data":map({"SNAME":.})}
{"data":[{"SNAME":"a"},{"SNAME":"b"},{"SNAME":"c"}]}
Powershell
Опять же, кроме хеша и фигурных скобок, возможны простые решения:
Использование строк в одинарных кавычках:
echo '["a", "b", "c"]' | jq -c '{"data": map( {"SNAME": . })}'
{"data":[{"SNAME":"a"},{"SNAME":"b"},{"SNAME":"c"}]}
С помощью ""
внутри строк в двойных кавычках:
echo '["a", "b", "c"]' | jq -c "{""data"": map( {""SNAME"": . })}"
{"data":[{"SNAME":"a"},{"SNAME":"b"},{"SNAME":"c"}]}
Документация по PowerShell, которую я видел, предлагает использовать обратные метки для экранирования специальных символов в строках с двойными кавычками, кроме YMMV.
Боннский шанс!
- Remove From My Forums
-
Вопрос
-
every time i run this commad —> cat combined.json | jq -r ‘.contracts.»MultiSigWallet.sol:MultiSigWallet».abi’, i get an error
jq: error: syntax error, unexpected ‘:’, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
.contrats.MultiSigWallet.sol:MultiSigWallet.abi
jq: 1 compile error-
Изменено
tnlthanzeel
2 мая 2019 г. 6:27
-
Изменено
Все ответы
-
You will have to ask in a JQ forum for help with non PS errors. This forum does not support third party utilities.
\_(ツ)_/
-
Предложено в качестве ответа
LeeSeenLiMicrosoft contingent staff
3 мая 2019 г. 6:06
-
Предложено в качестве ответа
-
I believe he is using cmd and not powershell (single quote problem). https://github.com/stedolan/jq/wiki/FAQ#windows
Hmm, maybe even in powershell you have to backslash the doublequotes.
«PowerShell stripping double quotes from command line arguments» [for some external programs]
cat combined.json | jq -r ‘.contracts.\»MultiSigWallet.sol:MultiSigWallet\».abi’
-
Изменено
JS2010
4 мая 2019 г. 15:00
-
Изменено
-
«cat» is PowerShell. The forum is only for PowerShell. PowerShell does not use a backslash to escape quotes.
The issue must be handled by the tool vendor as it is not a PowerShell issue.
\_(ツ)_/
-
Предложено в качестве ответа
LeeSeenLiMicrosoft contingent staff
3 мая 2019 г. 6:06
-
Предложено в качестве ответа
-
Hi,
Was your issue resolved?
If you resolved it using our solution, please «mark it as answer» to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Just do it.