首页 > 解决方案 > 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?

标签: jq

解决方案


select(.requested_reviewers != [])

推荐阅读