100

I am using jq but having "-" in my json tag make jq not compile. I cannot escape it to make it works. Here is the command:

curl -X GET -H "X-AppKey:foo" "foo/v2/_status" | jq '.component-status[]'

I have read in the github of jq this post https://github.com/stedolan/jq/issues/202 but I cannot make it work.

This is the output of the curl:

{
  "status": "ok",
  "hostname": "0b0b495a46db",
  "component-status": [
   {
     "status-code": 200,
     "component": "Service1",
     "status": "OK"
   },
   {
     "status-code": 200,
     "component": "Service2",
     "status": "OK"
   }
  ]
 }

Any idea?

0

2 Answers 2

188

You need to enclose in brackets and double quotes:

jq '."component-status"'

With your given input it returns:

[
  {
    "status": "OK",
    "component": "Service1",
    "status-code": 200
  },
  {
    "status": "OK",
    "component": "Service2",
    "status-code": 200
  }
]

The jq Manual (development) --> Basic filters:

.foo, .foo.bar

The simplest useful filter is .foo. When given a JSON object (aka dictionary or hash) as input, it produces the value at the key “foo”, or null if there’s none present.

If the key contains special characters, you need to surround it with double quotes like this: ."foo$".

From the github issue Cannot select field if field name has dashes:

Currently, that gets parsed as a subtraction. You can always explicitly use strings for when your keys don't fit identifier syntax.

Sign up to request clarification or add additional context in comments.

9 Comments

Using the form .["quoted-name"] has the advantage of working in all versions of jq. In jq version 1.4 and up, the shorter form ."quoted-name" is also supported
@peak thanks for the insight! Do you have links for that, so I can add to the answer? My jq-fu is limited and all I did here was try a bit and find the explanation in a github issue from the jq project.
The "development version" of the manual (stedolan.github.io/jq/manual) has links to other versions. The jq FAQ (github.com/stedolan/jq/wiki/FAQ) also discusses the issue. From the "development version": > If the key contains special characters, you need to surround it with double quotes like this: ."foo$".
I've run into cases where the square brackets (.["hyphenated-string"]) actually causes an error: jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>. Whereas just adding double quotes (."hyphenated-string") worked fine.
@BrianV You can repro with this: echo '{"a-a":{"b-b": "huzzah"}}'| jq '.["a-a"]."b-b"'. As typed, it works. But if you put a [ ] around "b-b" you get that error. '."a-a"."b-b"' also works.
|
11

The option suggested by rjurney or the commenters to his answers did not work for me (probably because I used PowerShell), however in the answer from github issue there was a solution, which did the trick - escaping double quotation marks with \

jq '.\"component-status\"'

3 Comments

@DivDev - Please be explicit about the computing environment you have in mind. This solution will not work in bash and bash-like shells.
@peak, it was PowerShell, I added it to the answer
It works on redhat 8 also

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.