74

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
paul
  • 12,873
  • 23
  • 91
  • 153

2 Answers2

146

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.

rjurney
  • 4,824
  • 5
  • 41
  • 62
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    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 May 20 '16 at 20:22
  • @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. – fedorqui May 20 '16 at 22:40
  • 2
    The "development version" of the manual (https://stedolan.github.io/jq/manual/) has links to other versions. The jq FAQ (https://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$". – peak May 21 '16 at 02:31
  • 4
    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 `. Whereas just adding double quotes (`."hyphenated-string"`) worked fine. – BrianV Apr 27 '18 at 15:14
  • 1
    @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. – Trenton Sep 06 '19 at 00:19
  • @BrianV I have this issue too, so I've edited the answer to use '."key-name"' instead of brackets, which bash can't handle. – rjurney Jun 19 '20 at 23:33
  • @Trenton fixed :) – rjurney Jun 19 '20 at 23:35
  • @Trenton - The square-bracket form `.["KEYNAME"]` works in all versions of jq (unlike the ."KEYNAME" form), but to abbreviate `.["a-a"]|.["b-b"]` you would have to write `.["a-a"]["b-b"]` -- that is, drop the "|" and the ".". – peak Jul 06 '21 at 07:58
6

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\"'
Dim Dev
  • 63
  • 1
  • 4