Anthony Kong
Anthony Kong

Reputation: 40624

How to use jq to not show a json data if a field is an empty array?

I have some github data in json format like so:

{
  "id": 18535,
  "node_id": "MDExhN0M8877883",
  "number": 747,
  "state": "closed",
  "locked": false,
  "title": "Generate cordova protos",
  "user": {
    "login": "Superhacker"
  },
  "body": "Simple PR",
  "created_at": "2018-05-02T07:43:39Z",
  "updated_at": "2018-07-21T02:09:14Z",
  "closed_at": "2018-05-02T07:54:56Z",
  "merge_commit_sha": "4bf92c0332c66999999b6c0a766e",
  "assignee": null,
  "assignees": [],
  "requested_reviewers": [],
  "requested_teams": [],
  "labels": [],
  "milestone": null,
  "draft": false,
  "author_association": "CONTRIBUTOR"
}

I want to NOT showing the json strings when the requested_reviewers is empty.

I have tried several ways

  1. echo <the json string above> | jq '.requested_reviewers | select(. != "")

It shows []

  1. echo <the json string above> | jq '.requested_reviewers | select(! empty)

It gave a syntax error.

  1. echo <the json string above> | jq '.requested_reviewers | select(. != empty)

Now jq does not print out anything as I wanted.

But if I feed a json with non empty requested_reviewers, it also does not print anything.

Besides I don't want to just print out the field. I want to be able to see the entire json string.

How can I achieve my goal with jq?

Upvotes: 1

Views: 2369

Answers (2)

Thomas__
Thomas__

Reputation: 330

jq 'select(.requested_reviewers | length > 0)'

https://jqplay.org/s/Cw-nbWwmjz

Upvotes: 3

peak
peak

Reputation: 116680

select(.requested_reviewers != [])

Upvotes: 7

Related Questions