eljoeyjojo
eljoeyjojo

Reputation: 61

How to join two values to print in one line using jq

I have this list that prints out using the gh api command:

    [
  {
    "login": "anthonyd-cd",
    "id": 35614549,
    "node_id": "MDQ6VXNlcjM1NjE0NTQ5",
    "avatar_url": "https://avatars.githubusercontent.com/u/35614549?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/anthonyd-cd",
    "html_url": "https://github.com/anthonyd-cd",
    "followers_url": "https://api.github.com/users/anthonyd-cd/followers",
    "following_url": "https://api.github.com/users/anthonyd-cd/following{/other_user}",
    "gists_url": "https://api.github.com/users/anthonyd-cd/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/anthonyd-cd/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/anthonyd-cd/subscriptions",
    "organizations_url": "https://api.github.com/users/anthonyd-cd/orgs",
    "repos_url": "https://api.github.com/users/anthonyd-cd/repos",
    "events_url": "https://api.github.com/users/anthonyd-cd/events{/privacy}",
    "received_events_url": "https://api.github.com/users/anthonyd-cd/received_events",
    "type": "User",
    "site_admin": false,
    "contributions": 5
  },
  {
    "login": "elJoeyJojo",
    "id": 17091968,
    "node_id": "MDQ6VXNlcjE3MDkxOTY4",
    "avatar_url": "https://avatars.githubusercontent.com/u/17091968?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/elJoeyJojo",
    "html_url": "https://github.com/elJoeyJojo",
    "followers_url": "https://api.github.com/users/elJoeyJojo/followers",
    "following_url": "https://api.github.com/users/elJoeyJojo/following{/other_user}",
    "gists_url": "https://api.github.com/users/elJoeyJojo/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/elJoeyJojo/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/elJoeyJojo/subscriptions",
    "organizations_url": "https://api.github.com/users/elJoeyJojo/orgs",
    "repos_url": "https://api.github.com/users/elJoeyJojo/repos",
    "events_url": "https://api.github.com/users/elJoeyJojo/events{/privacy}",
    "received_events_url": "https://api.github.com/users/elJoeyJojo/received_events",
    "type": "User",
    "site_admin": false,
    "contributions": 2
  }
]

I am trying to use jq to print out only the login and contributions field but into one line like so:

gh api repos/eljoeyjojo/diner-app/contributors | jq -r '.[].login, .[].contributions'

Expected Result

anthony-cd 5
eljoeyjojo 2

Actual Result

anthony-cd
eljoeyjojo
5
2

Any idea how I could print/merge these two fields? Would I have to use a for loop in this case?

Thanks

Upvotes: 0

Views: 236

Answers (1)

pmf
pmf

Reputation: 36033

You are using the iterator .[] twice, so each field is extracted from a different iteration. Pull the iterator up front and extract from the context of an iteration step. Then you can use various methods to combine those, one being string interpolation "\(...)".

jq -r '.[] | "\(.login) \(.contributions)"'
anthonyd-cd 5
elJoeyJojo 2

Demo

Upvotes: 1

Related Questions