jq icon indicating copy to clipboard operation
jq copied to clipboard

Special character like '-' in key not able parse on bash cmd or script file

Open venkatesanvp opened this issue 7 years ago • 10 comments

$ echo '{"p-name": "Dr Nic"}' | jq -r .p-name error: name is not defined .p-name ^^^^ 1 compile error

venkatesanvp avatar Sep 03 '16 19:09 venkatesanvp

  1. 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"]
  2. The jq manual mentions other alternatives.
  3. This "Issues" web site has several entries on the topic.
  4. In future, please try googling for an answer. In the present case, you would have had some success using these terms: dash jq error

pkoppstein avatar Sep 03 '16 19:09 pkoppstein

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.

kavehmz avatar Apr 04 '18 12:04 kavehmz

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?

veleek avatar May 05 '20 21:05 veleek

can you try this?

echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '."Bar-Baz"'

Double quotation around "Bar-Baz" after ..

kavehmz avatar May 06 '20 06:05 kavehmz

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

veleek avatar May 06 '20 06:05 veleek

How about jq ".""Bar-Baz""" or jq ^".\^"Bar-Baz\^"^" ?

itchyny avatar May 06 '20 08:05 itchyny

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?

veleek avatar May 07 '20 05:05 veleek

Oh sorry, ^ is the escape character in cmd. Did you try jq '.\"Bar-Baz\"'?

itchyny avatar May 07 '20 05:05 itchyny

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!

veleek avatar May 07 '20 05:05 veleek

echo '{"Foo":"value1", "Bar-Baz":"value2"}' | jq '."Bar-Baz"'

basdemir avatar Jul 15 '22 12:07 basdemir

How can we pass "Bar-Baz" as a parameter to the shell script

PrathapJ4 avatar May 10 '23 07:05 PrathapJ4

Are you talking about as a powershell parameter or something else?

veleek avatar May 10 '23 19:05 veleek

power shell parameter

PrathapJ4 avatar Jun 01 '23 14:06 PrathapJ4