jq
jq copied to clipboard
Special character like '-' in key not able parse on bash cmd or script file
$ echo '{"p-name": "Dr Nic"}' | jq -r .p-name error: name is not defined .p-name ^^^^ 1 compile error
- When property names have special characters (such as dash) in them, some care is needed. If P is a property name (such as
foo
) you can write.["P"]
; in your case, you could write.["p-name"]
- The jq manual mentions other alternatives.
- This "Issues" web site has several entries on the topic.
- In future, please try googling for an answer. In the present case, you would have had some success using these terms: dash jq error
Just in case you landed this page, don't forget the '
around the whole path.
echo '{"p-name": "Dr Nic"}' | jq -r '.["p-name"]'
Also look at manual as mentioned in previous comment. All explained there.
I'm unable to get this working at all on Windows despite many attempts to quote it properly. Can someone point out to me if I'm doing something wrong?
PS C:\Users\benran> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq ".Foo"
"value1"
PS C:\Users\benran> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq ".Bar-Baz"
jq: error: Baz/0 is not defined at <top-level>, line 1:
.Bar-Baz
jq: 1 compile error
PS C:\Users\benran> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq ".`"Bar-Baz`""
jq: error: Baz/0 is not defined at <top-level>, line 1:
.Bar-Baz
jq: 1 compile error
PS C:\Users\benran> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq ".[`"Bar-Baz`"]"
jq: error: Bar/0 is not defined at <top-level>, line 1:
.[Bar-Baz]
jq: error: Baz/0 is not defined at <top-level>, line 1:
.[Bar-Baz]
jq: 2 compile errors
PS C:\Users\benran> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '.["Bar-Baz]'
jq: error: Bar/0 is not defined at <top-level>, line 1:
.[Bar-Baz]
jq: error: Baz/0 is not defined at <top-level>, line 1:
.[Bar-Baz]
jq: 2 compile errors
PS C:\Users\benran> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '.["Bar-Baz"]'
jq: error: Bar/0 is not defined at <top-level>, line 1:
.[Bar-Baz]
jq: error: Baz/0 is not defined at <top-level>, line 1:
.[Bar-Baz]
jq: 2 compile errors
PS C:\Users\benran>
This is on pwsh on Windows. Is there no way to get the quotes to pass through properly?
can you try this?
echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '."Bar-Baz"'
Double quotation around "Bar-Baz"
after .
.
Same result as
echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq ".`"Bar-Baz`""
PS C:\Users\veleek> echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '."Bar-Baz"'
jq: error: Baz/0 is not defined at <top-level>, line 1:
.Bar-Baz
jq: 1 compile error
How about jq ".""Bar-Baz"""
or jq ^".\^"Bar-Baz\^"^"
?
Nope neither of those work either. What is the ^
escaping for? I've never seen that for powershell.
For reference: The following works in cmd on windows, just to prove that there's nothing wrong with the actual jq binary, and it's just an issue with the way powershell/pwsh is passing the string to jq.
C:\Users\veleek> echo {"Foo":"value1", "Bar-Baz":"value2"} | jq ".""Bar-Baz"""
"value2"
Is there any way to have jq dump out the filter it's receiving before parsing it?
Oh sorry, ^
is the escape character in cmd. Did you try jq '.\"Bar-Baz\"'
?
Grrrr.... literally JUST tried that after finally figuring out the right thing to google and finding this Stackoverflow question from nine years ago which very clearly explains the problem and the solution. :P
Very annoying to have to remember another escaping mechanism, but at least it works! I'm going to solve this problem by aliases the jq command and having pwsh automatically add \
escapes to any double quotes in the string. Not perfect, but it works pretty well.
Thanks for the help!
echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '."Bar-Baz"'
How can we pass "Bar-Baz" as a parameter to the shell script
Are you talking about as a powershell parameter or something else?
power shell parameter