1

Whole file:https://1drv.ms/u/s!AizscpxS0QM4hJpEkp12VPHiKO_gBg

Using this command i get part bellow (get latest job)

jq '.|[ .executions[] | select(.job.name != null) | select(.job.name) ]
     | sort_by(.id)
     | reverse
     | .[0] ' 1.json



{
  "argstring": null,
  "date-ended": {
    "date": "2018-04-03T17:43:38Z",
    "unixtime": 1522777418397
  },
  "date-started": {
    "date": "2018-04-03T17:43:34Z",
    "unixtime": 1522777414646
  },
  "description": "",
  "executionType": "user",
  "failedNodes": [
    "172.30.61.88"
  ],
  "href": "http://172.30.61.88:4440/api/21/execution/126",
  "id": 126,
  "job": {
    "averageDuration": 4197,
    "description": "",
    "group": "",
    "href": "http://172.30.61.88:4440/api/21/job/271cbcec-5042-4d52-b794-ede2056b2ab8",
    "id": "271cbcec-5042-4d52-b794-ede2056b2ab8",
    "name": "aa",
    "permalink": "http://172.30.61.88:4440/project/demo/job/show/271cbcec-5042-4d52-b794-ede2056b2ab8",
    "project": "demo"
  },
  "permalink": "http://172.30.61.88:4440/project/demo/execution/show/126",
  "project": "demo",
  "status": "failed",
  "user": "administrator"

I managed to extract job name and status, now want to get date-ended.date ?

jq '.|[ .executions[] |select(.job.name != null) | select(.job.name) ]
     | sort_by(.id)
     | reverse 
     | .[0]
     | "\(.status), \(.job.name)"' 1.json
peak
  • 105,803
  • 17
  • 152
  • 177
Milister
  • 648
  • 1
  • 15
  • 33
  • In the future, it would be helpful to write a question more focused on providing a minimal reproducer for the specific thing you're having trouble with. (For instance, "how do I extract a property with a dash in its name in `jq`?" would be a great question, if we don't already have it in the knowledgebase). – Charles Duffy Apr 03 '18 at 19:30
  • ...in that particular case, we *do* have it already, [`jq` not working on tag name with dashes](https://stackoverflow.com/questions/37344329/jq-not-working-on-tag-name-with-dashes), or also as [`jq` not working with key including dash](https://stackoverflow.com/questions/48395816/jq-not-working-with-key-including-dash?noredirect=1&lq=1). – Charles Duffy Apr 03 '18 at 19:31

2 Answers2

1

With the "-r" command-line option, the following filter:

 [.executions[] | select(.job.name != null)]
 | sort_by(.id)
 | reverse 
 | .[0]
 | [.status, .job.name, ."date-ended".date]
 | @csv

produces:

"failed","aa","2018-04-03T17:43:38Z"

An important point that you might have missed is that "-" is a "special" character in that it can signify negation or subtraction.

If your jq does not support the syntax ."date-ended".date, then you could fall back to the basic syntax: (.["date-ended"] | .date)

peak
  • 105,803
  • 17
  • 152
  • 177
  • Having dug into the OP's code enough to grok their question properly -- was the only open question about the usage of dashes in keys (such that this could be closed as a duplicate), or are there novel/interesting/useful aspects raised (which could probably stand to be isolated and extracted so others can more easily learn from the question)? – Charles Duffy Apr 03 '18 at 19:31
  • 1
    @CharlesDuffy - Evidently the OP wasn't able to zero-in on the actual problem, and may have been confused by the differing behavior of different versions of jq. The OP's code contained some other infelicities that have been (covertly) addressed, but by all means, close the question. – peak Apr 03 '18 at 19:51
0

I guess you have troubles extracting .date-ended.date because the name contains a dash that is interpreted by jq as subtraction.

The solution is listed in the documentation:

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

This means the last filter of your jq program should be:

"\(.status), \(.job.name), \(."date-ended".date)"
axiac
  • 68,258
  • 9
  • 99
  • 134
  • yes, i tried that way – Milister Apr 03 '18 at 19:34
  • *If* how to index into a key with dashes contained is all the OP is asking, then there are multiple duplicates. (The only reason I haven't closed the question as a duplicate already is that it's complex enough that it's hard to tell if there are other aspects; that argues for other close reasons, but not dupehammer-eligible ones). Thus, if this answer is responsive, then the question should have been closed, not answered. – Charles Duffy Apr 03 '18 at 19:35