paul paul - 6 months ago 13
JSON Question

jq not working on tag name with dashes

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

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


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

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?

Regards.

Answer

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.