Comments (26)
jq '.stuff["thisisakey-key"].identifier'
http://stedolan.github.io/jq/manual/#foo it's similar in JavaScript
from jq.
Just for posterity, the following seems to work:
jq .\"a-b\"
Seems like the shortest working query string with a hyphen.
from jq.
Okay, so, to settle this question already:
If the key has no special characters, access it like .this
.
If the key has special characters, access it like ["this"]
.
So, @zasran, what you're looking for is .["a-b"]["c-d"]
.
from jq.
Currently, that gets parsed as a subtraction. You can always explicitly use strings for when your keys don't fit identifier syntax:
{"my-new-field": .["my-old-field"]}
from jq.
Wrap hyphenated name in escaped quotation marks.
jq "[.[]|{\"my-field\"}]" test.json
[
{
"my-field": "val1"
},
{
"my-field": "val2"
}
]
from jq.
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"'
error: syntax error, unexpected QQSTRING_START, expecting $end
."a-b"
^
1 compile error
This works:
$ echo '{"a-b":"0"}' | jq '.["a-b"]'
"0"
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"'
error: syntax error, unexpected QQSTRING_START, expecting IDENT
.["a-b"]."c-d"
^
1 compile error
$ echo '{"a-b":{"c-d":0}}' | jq '.["a-b"].["c-d"]'
error: syntax error, unexpected '[', expecting IDENT
.["a-b"].["c-d"]
^
1 compile error
Two questions/possible bugs:
Should ."a-b" work (in the example above)?
How to specify c-d (i.e. nested key with special characters)?
from jq.
If you need to pass a variable containing a string with a dash and save it in a variable in a bash script...
result=$(jq --arg a "$query" '.[$a]' -r some.json)
from jq.
thanks @pkoppstein - that helps, and this works:
echo '{ "foo-bar":"baz" }' | jq '.[ "foo-bar" ]'
from jq.
key="foo-bar"; echo '[{ "foo-bar":"baz" }]' | jq '.[0].[ "$key" ]'
There are two problems with the above. First, the abbreviation .[0].["KEYNAME"]
is not (currently) supported. The second has to do with the relationship between the shell or environment variable $key and the jq variable $key
.
In a nutshell, here is what is recommended:
key="foo-bar"; echo '[{ "foo-bar":"baz" }]' | jq --arg key "$key" '.[0]|.[ $key ]'
For future reference, please ask usage questions at stackoverflow.com with the jq tag: https://stackoverflow.com/questions/tagged/jq
from jq.
In fact, it seems like it would be more correct if, in your parser, you would prioritize matching key names over evaluating operators.
example: echo '{"key-10": 10}' | jq ".key-10"
Ideally would print "10"... and:
echo '{"key-10": 10}' | jq ".key-10-10"
would print "0". I say this of course, after having thought about all the possibilities for a full 5 minutes or so :-)
from jq.
from jq.
Guess that makes sense, I just didn't stumble across that syntax :)
from jq.
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:
error: key is not defined
.stuff.thisisakey-key.identifier
^^^
1 compile error
How can I do it? I tried with "" and so on, but none of them works.
from jq.
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"]'
319
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"'
error: syntax error, unexpected QQSTRING_START, expecting $end
."foo"
^
1 compile error
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?
from jq.
Dot (.
) refers to "the input to the current expression", roughly.
The ."string"
syntax (equivalent to .["string"]
) is not in a released version, but it is in the master branch.
from jq.
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.
from jq.
@nicowilliams thanks, that clarifies it!
from jq.
Hi all,
@nicowilliams
thank you for the great job, this tool really saves me a lot of time.
Unfortunately I am facing this issue, and it seems that a release was planned for soon.
correct me if I am wrong but 1.3 is 1year old.
Do you have an estimate date for 1.4 ?
By the way could you please summarise if a workaroud exist for 1.3 ?
Here is my syntax:
jq ".name.PS_Perm_Gen.peak-usage.init" < /tmp/localhost-localhost-9990-Memory
And, I've tried:
jq .["name.PS_Perm_Gen.peak-usage.init"] < /tmp/localhost-localhost-9990-Memory
error: Invalid numeric literal (while parsing '..')
and that:
jq .name.PS_Perm_Gen.["peak-usage"].init < /tmp/localhost-localhost-9990-Memory
error: syntax error, unexpected '[', expecting IDENT
.name.PS_Perm_Gen.[peak-usage].init
both does not work
greetings
Akram
from jq.
I didn't finish setting up build envs for Win64 in time and then business
travel intervened. I'll get to it soon, but not this week.
from jq.
@akram jq .name.PS_Perm_Gen["peak-usage"].init
is what you're looking for.
There are two syntaxes for element access: .foo
and ["foo"]
. Note the lack of leading dot in the second case. The second syntax allows for extraneous characters, the first one does not.
If you're using master, as well as in the future 1.4 version, ."foo"
is a valid option as well. It also allows extraneous characters.
from jq.
I think it's important to note that if you ever script the query string to jq
, it would be unsafe in the generic case to not use the jq ".[$keyname]"
notation if the names of your keys are not statically known/defined. A simple jq ".$keyname"
would break if $keyname ever has a hyphen in it.
from jq.
Then you have a problem if someone wants to subtract 10 from a key:
echo '{"key": 11, "key-10": 12}' | jq ".${KEY}-10"
This would require the even more confusing ".${KEY}-(10)"
or "(.${KEY})-10"
.
from jq.
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?
from jq.
@mcwumbly - The short of it is that, given a JSON object, it's always safe to use the form: .[ $keyname ]
where $keyname
is a JSON string.
Note also that the fact that .X | .Y
works for some X and Y does NOT always mean that .X.Y
will work.
For example, .[0] | .["foo-bar"]
is valid, but .[0].["foo-bar"]
is not.
Alternatively:
$echo '[{ "foo-bar":"baz" }]' | jq '.[0][ "foo-bar" ]'
"baz"
from jq.
from jq.
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
from jq.
Related Issues (20)
- Cannot select sub-field that has a dash in the name of the field HOT 1
- compile error HOT 10
- Multiplying -1 with 0 must result 0, not -0 HOT 2
- ~/.jq is not sourced on windows
- Unexpected output HOT 1
- Null bytes are handled inconsistently HOT 5
- tonumber doesn't work on `true` or `false` HOT 4
- "color for object keys" from JQ_COLORS doesn't seem to be respected HOT 4
- `range/3` behaviour when $init and $upto arguments are not numbers HOT 4
- Regular expression alternation (|) used with quantifier (* or +) returns inconsistent results when first alternative is able to match an empty string HOT 3
- Create Multi Smaller Files From a Big Json File HOT 5
- Add the Symbolic Binding Operator ("as") to the operator priority table HOT 1
- Including and importing module with importing JSON crashes jq
- The code example for trim ltrim and rtrim from the manual do not work HOT 1
- Incorrect `Invalid path expression` in destructuring expressions
- 1.7.1 dumps core on Cygwin, trying to find the input filename to print an error message HOT 5
- jq --raw modes, --binary and newlines
- Request for snap maintenance HOT 11
- strptime month does not match input HOT 2
- Using unique or unique_by in a filter causes subsequent (after comma ,) filter inputs to be replaced with its output HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jq.