Giter VIP home page Giter VIP logo

Comments (26)

 avatar commented on June 26, 2024 117

jq '.stuff["thisisakey-key"].identifier' http://stedolan.github.io/jq/manual/#foo it's similar in JavaScript

from jq.

hexsel avatar hexsel commented on June 26, 2024 54

Just for posterity, the following seems to work:

jq .\"a-b\"

Seems like the shortest working query string with a hyphen.

from jq.

 avatar commented on June 26, 2024 33

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.

stedolan avatar stedolan commented on June 26, 2024 27

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.

hughmandeville avatar hughmandeville commented on June 26, 2024 11

Wrap hyphenated name in escaped quotation marks.

jq "[.[]|{\"my-field\"}]" test.json 
[
  {
    "my-field": "val1"
  },
  {
    "my-field": "val2"
  }
]

from jq.

zasran avatar zasran commented on June 26, 2024 9

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.

dhjw avatar dhjw commented on June 26, 2024 7

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.

mcwumbly avatar mcwumbly commented on June 26, 2024 4

thanks @pkoppstein - that helps, and this works:

echo '{ "foo-bar":"baz" }' | jq '.[ "foo-bar" ]'

from jq.

pkoppstein avatar pkoppstein commented on June 26, 2024 2

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.

ereyes01 avatar ereyes01 commented on June 26, 2024 1

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.

nicowilliams avatar nicowilliams commented on June 26, 2024 1

from jq.

ilkka avatar ilkka commented on June 26, 2024

Guess that makes sense, I just didn't stumble across that syntax :)

from jq.

woohgit avatar woohgit commented on June 26, 2024

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.

zasran avatar zasran commented on June 26, 2024

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.

nicowilliams avatar nicowilliams commented on June 26, 2024

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.

nicowilliams avatar nicowilliams commented on June 26, 2024

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.

zasran avatar zasran commented on June 26, 2024

@nicowilliams thanks, that clarifies it!

from jq.

akram avatar akram commented on June 26, 2024

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.

nicowilliams avatar nicowilliams commented on June 26, 2024

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.

 avatar commented on June 26, 2024

@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.

ereyes01 avatar ereyes01 commented on June 26, 2024

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.

dtolnay avatar dtolnay commented on June 26, 2024

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.

mcwumbly avatar mcwumbly commented on June 26, 2024

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.

pkoppstein avatar pkoppstein commented on June 26, 2024

@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.

wtlangford avatar wtlangford commented on June 26, 2024

from jq.

pierreprinetti avatar pierreprinetti commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.