paul
paul

Reputation: 13471

jq not working on tag name with dashes and numbers

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?

Upvotes: 95

Views: 58016

Answers (2)

Dim Dev
Dim Dev

Reputation: 103

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\"'

Upvotes: 10

fedorqui
fedorqui

Reputation: 289745

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.

Upvotes: 180

Related Questions