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
}
"설명"을 어떻게 추출합니까?
"언어"와 "설명"을 추출하는 방법은 무엇입니까?
질문 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
두 개의 변수 할당을 포함하는 쉘 코드를 생성 해 보겠습니다 . 이 값은 쉘에서 올바르게 인용됩니다.
$ jq -r '@sh "lang=\(.language)", @sh "desc=\(.description)"' file.json
lang='Shell'
desc='Tmux Plugin Manager'
쉘이 실제로 다음 명령문을 평가하도록 하십시오.
$ 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
- "설명"을 어떻게 추출합니까?
$ curl -s 'https://api.github.com/repos/tmux-plugins/tpm' | jq -r '.description'
Tmux Plugin Manager
- "언어"와 "설명"을 추출하는 방법은 무엇입니까?
$ curl -s 'https://api.github.com/repos/tmux-plugins/tpm'
| jq -r '.language + ": " + .description'
Shell: Tmux Plugin Manager
두 번째 터미널 명령은 다음과 같아야 합니다.하나열.
이 두 줄을 한 번에 복사하여 붙여넣은 경우
shift+를 사용하여 insert터미널에 붙여넣어 보세요.
노트:-r
답변을 문자열로 얻으려면 플래그를 제거하면 됩니다.