如何使用jq从github api中提取信息字段

如何使用jq从github api中提取信息字段

我正在尝试使用其 API 提取有关 github 存储库的一些信息,显然jq这是可行的方法。我可以使用此命令查看所有可用信息:

curl 'https://api.github.com/repos/tmux-plugins/tpm' | jq

输出:

{
  "id": 19935788,
  "node_id": "MDEwOlJlcG9zaXRvcnkxOTkzNTc4OA==",
  "name": "tpm",
  "full_name": "tmux-plugins/tpm",
  "private": false,
  "owner": {
    "login": "tmux-plugins",
    "id": 8289877,
    "node_id": "MDEyOk9yZ2FuaXphdGlvbjgyODk4Nzc=",
    "avatar_url": "https://avatars.githubusercontent.com/u/8289877?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/tmux-plugins",
    "html_url": "https://github.com/tmux-plugins",
    "followers_url": "https://api.github.com/users/tmux-plugins/followers",
    "following_url": "https://api.github.com/users/tmux-plugins/following{/other_user}",
    "gists_url": "https://api.github.com/users/tmux-plugins/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/tmux-plugins/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/tmux-plugins/subscriptions",
    "organizations_url": "https://api.github.com/users/tmux-plugins/orgs",
    "repos_url": "https://api.github.com/users/tmux-plugins/repos",
    "events_url": "https://api.github.com/users/tmux-plugins/events{/privacy}",
    "received_events_url": "https://api.github.com/users/tmux-plugins/received_events",
    "type": "Organization",
    "site_admin": false
  },
  "html_url": "https://github.com/tmux-plugins/tpm",
  "description": "Tmux Plugin Manager",
  "fork": false,
  "url": "https://api.github.com/repos/tmux-plugins/tpm",
  "forks_url": "https://api.github.com/repos/tmux-plugins/tpm/forks",
  "keys_url": "https://api.github.com/repos/tmux-plugins/tpm/keys{/key_id}",
  "collaborators_url": "https://api.github.com/repos/tmux-plugins/tpm/collaborators{/collaborator}",
  "teams_url": "https://api.github.com/repos/tmux-plugins/tpm/teams",
  "hooks_url": "https://api.github.com/repos/tmux-plugins/tpm/hooks",
  "issue_events_url": "https://api.github.com/repos/tmux-plugins/tpm/issues/events{/number}",
  "events_url": "https://api.github.com/repos/tmux-plugins/tpm/events",
  "assignees_url": "https://api.github.com/repos/tmux-plugins/tpm/assignees{/user}",
  "branches_url": "https://api.github.com/repos/tmux-plugins/tpm/branches{/branch}",
  "tags_url": "https://api.github.com/repos/tmux-plugins/tpm/tags",
  "blobs_url": "https://api.github.com/repos/tmux-plugins/tpm/git/blobs{/sha}",
  "git_tags_url": "https://api.github.com/repos/tmux-plugins/tpm/git/tags{/sha}",
  "git_refs_url": "https://api.github.com/repos/tmux-plugins/tpm/git/refs{/sha}",
  "trees_url": "https://api.github.com/repos/tmux-plugins/tpm/git/trees{/sha}",
  "statuses_url": "https://api.github.com/repos/tmux-plugins/tpm/statuses/{sha}",
  "languages_url": "https://api.github.com/repos/tmux-plugins/tpm/languages",
  "stargazers_url": "https://api.github.com/repos/tmux-plugins/tpm/stargazers",
  "contributors_url": "https://api.github.com/repos/tmux-plugins/tpm/contributors",
  "subscribers_url": "https://api.github.com/repos/tmux-plugins/tpm/subscribers",
  "subscription_url": "https://api.github.com/repos/tmux-plugins/tpm/subscription",
  "commits_url": "https://api.github.com/repos/tmux-plugins/tpm/commits{/sha}",
  "git_commits_url": "https://api.github.com/repos/tmux-plugins/tpm/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/tmux-plugins/tpm/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/tmux-plugins/tpm/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/tmux-plugins/tpm/contents/{+path}",
  "compare_url": "https://api.github.com/repos/tmux-plugins/tpm/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/tmux-plugins/tpm/merges",
  "archive_url": "https://api.github.com/repos/tmux-plugins/tpm/{archive_format}{/ref}",
  "downloads_url": "https://api.github.com/repos/tmux-plugins/tpm/downloads",
  "issues_url": "https://api.github.com/repos/tmux-plugins/tpm/issues{/number}",
  "pulls_url": "https://api.github.com/repos/tmux-plugins/tpm/pulls{/number}",
  "milestones_url": "https://api.github.com/repos/tmux-plugins/tpm/milestones{/number}",
  "notifications_url": "https://api.github.com/repos/tmux-plugins/tpm/notifications{?since,all,participating}",
  "labels_url": "https://api.github.com/repos/tmux-plugins/tpm/labels{/name}",
  "releases_url": "https://api.github.com/repos/tmux-plugins/tpm/releases{/id}",
  "deployments_url": "https://api.github.com/repos/tmux-plugins/tpm/deployments",
  "created_at": "2014-05-19T09:18:38Z",
  "updated_at": "2021-03-03T04:30:43Z",
  "pushed_at": "2021-02-23T11:07:55Z",
  "git_url": "git://github.com/tmux-plugins/tpm.git",
  "ssh_url": "[email protected]:tmux-plugins/tpm.git",
  "clone_url": "https://github.com/tmux-plugins/tpm.git",
  "svn_url": "https://github.com/tmux-plugins/tpm",
  "homepage": null,
  "size": 204,
  "stargazers_count": 6861,
  "watchers_count": 6861,
  "language": "Shell",
  "has_issues": true,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": true,
  "has_pages": false,
  "forks_count": 251,
  "mirror_url": null,
  "archived": false,
  "disabled": false,
  "open_issues_count": 79,
  "license": {
    "key": "mit",
    "name": "MIT License",
    "spdx_id": "MIT",
    "url": "https://api.github.com/licenses/mit",
    "node_id": "MDc6TGljZW5zZTEz"
  },
  "forks": 251,
  "open_issues": 79,
  "watchers": 6861,
  "default_branch": "master",
  "temp_clone_token": null,
  "organization": {
    "login": "tmux-plugins",
    "id": 8289877,
    "node_id": "MDEyOk9yZ2FuaXphdGlvbjgyODk4Nzc=",
    "avatar_url": "https://avatars.githubusercontent.com/u/8289877?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/tmux-plugins",
    "html_url": "https://github.com/tmux-plugins",
    "followers_url": "https://api.github.com/users/tmux-plugins/followers",
    "following_url": "https://api.github.com/users/tmux-plugins/following{/other_user}",
    "gists_url": "https://api.github.com/users/tmux-plugins/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/tmux-plugins/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/tmux-plugins/subscriptions",
    "organizations_url": "https://api.github.com/users/tmux-plugins/orgs",
    "repos_url": "https://api.github.com/users/tmux-plugins/repos",
    "events_url": "https://api.github.com/users/tmux-plugins/events{/privacy}",
    "received_events_url": "https://api.github.com/users/tmux-plugins/received_events",
    "type": "Organization",
    "site_admin": false
  },
  "network_count": 251,
  "subscribers_count": 83
}
  1. 我如何提取“描述”?

  2. 如何提取“语言”和“描述”?

我之所以问问题 2,是因为我在网上看到过一些例子(当我试图为自己寻找答案时),这些例子显示了多个字段被提取到一个字段中,这对我和其他找到这个问题的人来说很有帮助。谢谢!

答案1

在以下所有情况下,file.json都是包含 JSON 文档的文件的名称。显然,您可以jq像在问题中所做的那样使用它,并从连接到 输出的管道中读取它curl

将请求的字段一一拉出:

$ jq -r '.description' file.json
Tmux Plugin Manager
$ jq -r '.language' file.json
Shell

-r上面(和下面)使用该选项来获取“原始数据”而不是 JSON 编码数据。

一次获取两者(如果其中任何一个包含嵌入的换行符,您将无法区分它们):

$ jq -r '.language, .description' file.json
Shell
Tmux Plugin Manager

将它们作为 CSV 记录获取(将被正确引用,以便 CSV 解析器可以解析嵌入的逗号和换行符,并且嵌入的双引号也将被 CSV 编码):

$ jq -r '[.language, .description] | @csv' file.json
"Shell","Tmux Plugin Manager"

制表符分隔(嵌入的换行符和制表符将分别显示为\n\t):

$ jq -r '[.language, .description] | @tsv' file.json
Shell   Tmux Plugin Manager

jq生成包含两个变量赋值的 shell 代码。这些值将为 shell 正确引用。

$ jq -r '@sh "lang=\(.language)", @sh "desc=\(.description)"' file.json
lang='Shell'
desc='Tmux Plugin Manager'

让 shell 实际评估这些语句:

$ eval "$( jq -r '@sh "lang=\(.language)", @sh "desc=\(.description)"' file.json )"
$ printf 'lang is "%s" and desc is "%s"\n' "$lang" "$desc"
lang is "Shell" and desc is "Tmux Plugin Manager"

答案2

  1. 我如何提取“描述”?
$ curl -s 'https://api.github.com/repos/tmux-plugins/tpm' | jq -r '.description'
Tmux Plugin Manager
  1. 如何提取“语言”和“描述”?
$ curl -s 'https://api.github.com/repos/tmux-plugins/tpm'
   | jq -r '.language + ": " + .description'
Shell: Tmux Plugin Manager

第二个终端命令最好应该是排。
如果您一次性复制粘贴这两行,请尝试使用 shift+insert粘贴到终端中。

笔记:-r如果您希望以字符串形式获取答案,只需删除该标志即可。

相关内容