-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Cannot select field if field name has dashes #38
Comments
Currently, that gets parsed as a subtraction. You can always explicitly use strings for when your keys don't fit identifier syntax:
|
Guess that makes sense, I just didn't stumble across that syntax :) |
This means I cannot get a value from a json if the key contains a dash: < json.json jq .stuff.thisisakey-key.identifier because it'll throws:
How can I do it? I tried with "" and so on, but none of them works. |
|
According to http://stedolan.github.io/jq/manual/ "If the key contains special characters, you need to surround it with double quotes like this: ."foo$"." However the following does not work: $ echo '{"a-b":"0"}' | jq '."a-b"' This works: $ echo '{"a-b":"0"}' | jq '.["a-b"]' However it's not clear how to make it work if you want to get a nested value: $ echo '{"a-b":{"c-d":0}}' | jq '.["a-b"]."c-d"' $ echo '{"a-b":{"c-d":0}}' | jq '.["a-b"].["c-d"]' Two questions/possible bugs: Should ."a-b" work (in the example above)? |
Okay, so, to settle this question already: If the key has no special characters, access it like If the key has special characters, access it like So, @zasran, what you're looking for is |
Thanks, .["a-b"]["c-d"] does work. It still looks a bit inconsistent, maybe I don't understand what dot does or something like that (I read the docs at http://stedolan.github.io/jq/manual/). When there are no special characters this works: $ echo '{"ab":{"cd":319}}' | jq '.ab.cd' But if there are special characters it works without the dot (dot actually causes error): $ echo '{"ab":{"c-d":319}}' | jq '.ab["c-d"]' This seems a bit inconsistent, why is there a dot separator there for non-special character keys and no dot separator when the ["c-d"] format is used? (maybe calling dot separator is not correct but not sure what to call it) On a similar note, why does the documentation claim that ."foo$" would work (Basic filters section)? As far as I can tell it claims that .foo should be replaced by ."foo$" if the key contains special characters. The .["foo"] version is also mentioned. But what you are saying is that it really should be ["foo$"](no dot). Example straight from the docs (last .foo example in Basic filters section)): echo '{"foo": 42}' | jq '."foo"' Can you please shed some light on this, maybe change the docs so that it's more obvious? Or maybe it's actual bug and it should work as described in the docs? |
Dot ( The |
I'll fix the docs to list what's in 1.3 and what's new since at some point. Also, I think we'll do a new release soon. |
@nicowilliams thanks, that clarifies it! |
Hi all, Do you have an estimate date for 1.4 ? By the way could you please summarise if a workaroud exist for 1.3 ? And, I've tried: and that: both does not work greetings |
I didn't finish setting up build envs for Win64 in time and then business |
@akram There are two syntaxes for element access: If you're using master, as well as in the future 1.4 version, |
Wrap hyphenated name in escaped quotation marks. jq "[.[]|{\"my-field\"}]" test.json [
{
"my-field": "val1"
},
{
"my-field": "val2"
}
] |
I think it's important to note that if you ever script the query string to |
In fact, it seems like it would be more correct if, in your parser, you would prioritize matching key names over evaluating operators. example: Ideally would print "10"... and:
would print "0". I say this of course, after having thought about all the possibilities for a full 5 minutes or so :-) |
Then you have a problem if someone wants to subtract 10 from a key:
This would require the even more confusing |
Just for posterity, the following seems to work:
Seems like the shortest working query string with a hyphen. |
I ran into this issue today and this workaround in the comment above doesn't seem to work for me in jq 1.3. Anyone have any tips for what works today when strings have dashes in them? |
@mcwumbly - The short of it is that, given a JSON object, it's always safe to use the form: Note also that the fact that For example, Alternatively:
|
I should also point out that jq-1.3 is quite old. jq-1.5 is the current
release, and jq-1.6 is hopefully released soon (pending mine or
@nicowilliams' schedules).
As for jq-1.3, is that the package provided by your system or by your
sysadmin? In the latter case, I'd recommend pestering them to upgrade you
to 1.5 (which is much nicer in a lot of ways, and should not have any
backwards incompatibilities). If the former... I'm going to guess you're on
Debian. ;)
…On Tue, Jul 11, 2017 at 2:13 PM pkoppstein ***@***.***> wrote:
Assuming the input is a JSON object, it's always safe to use the form: .[
$keyname ] where $keyname is a JSON string.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#38 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ADQ4V3oOzLiAKHI6P03w7MAXr1rDQffqks5sM7stgaJpZM4AN_5L>
.
|
thanks @pkoppstein - that helps, and this works: echo '{ "foo-bar":"baz" }' | jq '.[ "foo-bar" ]' |
This works: echo '[{ "foo-bar":"baz" }]' | jq '.[0]' | jq '.[ "foo-bar" ]'
# "baz" This doesn't: echo '[{ "foo-bar":"baz" }]' | jq '.[0].[ "foo-bar" ]'
# jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
# .[0].[ "foo-bar" ]
# jq: 1 compile error |
Instead, this does not seem to work:
```shell
echo '[{ "foo-bar":"baz" }]' | jq '.[0].[ "foo-bar" ]'
```
You don't need that second period. Use `jq '.[0]["foo-bar"]'`.
That is, to chain index operations you must not use a period between the index operators. Only the first index operator needs a period (so as to distinguish it from the array constructor syntax).
We should probably make it so that extra period is not a syntax error, as this is a bit of an FAQ.
(Replying by email, so I think the formatting won't show correctly. But I'll set the MIME type to see if I can get it to work.)
|
There are two problems with the above. First, the abbreviation In a nutshell, here is what is recommended:
For future reference, please ask usage questions at stackoverflow.com with the jq tag: https://stackoverflow.com/questions/tagged/jq |
If you need to pass a variable containing a string with a dash and save it in a variable in a bash script...
|
jq does not like dashes see jqlang/jq#38 changelog_begin changelog_end
jq does not like dashes see jqlang/jq#38 changelog_begin changelog_end
Given this JSON:
this works:
but this does not:
I think it should since the JSON is valid. Maybe there is a way around this?
The text was updated successfully, but these errors were encountered: