Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq not working on tag name with dashes and numbers

Tags:

json

bash

jq

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?

like image 598
paul Avatar asked May 20 '16 10:05

paul


People also ask

Is hyphen allowed in JSON key?

Currently, if the key name in a json key/value pair contains a special character (e.g. periods/dots, colons, hyphens) the data pills are broken as a result and the core use case of using Workato to pass data between steps is broken.

How do you pass arguments to jq?

Passing arguments to JQ is done with --arg <name> <value> as shown below. Inside the filter, you can access a --arg with $<name> . In this case $foo returns bar . Note also in this example the -n flag which is used to tell JQ to not expect any JSON input.

Can jq write JSON?

jq is an amazing little command line utility for working with JSON data. We've written before about how you can use jq to parse JSON on the command line, but in this post I want to talk about using jq to create JSON data from scratch or make changes to existing data.

What is jq format?

jq is a free open source JSON processor that is flexible and straightforward to use. It allows users to display a JSON file using standard formatting, or to retrieve certain records or attribute-value pairs from it.


2 Answers

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.

like image 186
fedorqui 'SO stop harming' Avatar answered Sep 28 '22 03:09

fedorqui 'SO stop harming'


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\"' 
like image 25
Dim Dev Avatar answered Sep 28 '22 03:09

Dim Dev