Add `--json` and `--jq` to `gh pr create` · Issue #11247 · cli/cli · GitHub
Skip to content

Add --json and --jq to gh pr create #11247

Description

@dukeofgaming

Describe the feature or problem you’d like to solve

Add a --json response object with the ability to chain a jq query.

Proposed solution

This will avoid doing something like this to get the id or other metadata of a PR we just created:

PR_URL=$(gh pr \
  create \
  --base main \
  --head develop \
  --title "Promote dev to main" \
  --body "Automated promotion PR from dev to main" \
  --fill \
  | tail -n1)

PR_NUMBER=$(basename "$PR_URL")

And instead make it possible to do this:

PR_NUMBER=$(gh pr \
  create \
  --base main \
  --head develop \
  --title "Promote dev to main" \
  --body "Automated promotion PR from dev to main" \
  --json number,url \
  --jq '.[0].number'
)

Or this:

PR_NUMBER=$(gh pr \
  create \
  --base main \
  --head develop \
  --title "Promote dev to main" \
  --body "Automated promotion PR from dev to main" \
  --json number,url
)

PR_URL=$(echo "$PR_JSON" | jq -r '.[0].url')
PR_NUMBER=$(echo "$PR_JSON" | jq -r '.[0].number')

Additional context

Also, this would improve CLI API consistency

Pinned by BagToad

Activity

  1. babakks commented on Jul 9, 2025

    @babakks
    Member

    Hi @dukeofgaming, and thanks for your suggestion! 🙏

    IMO it would be a decent enhancement to gh pr create. However, historically, the team had reasons for dropping it (#6366, #11025). I believe there are complications around the implementation. For example, you can see the recent #11196 which is about adding the same --json option to gh issue create.

    That said, I'm going to do what we did with #11196 and label this for an internal discussion. I'll keep you posted.

  2. added
    coreThis issue is not accepting PRs from outside contributors
    discussFeature changes that require discussion primarily among the GitHub CLI team
    gh-prrelating to the gh pr command
    on Jul 9, 2025
  3. changed the title [-]Add --json and --jq to gh pr create[/-] [+]Add `--json` and `--jq` to `gh pr create`[/+] on Jul 18, 2025
  4. andyfeller commented on Oct 8, 2025

    @andyfeller
    Contributor

    🤔 Seeing this pop up in #11558, I want to ask "How should the Unix principle apply to this idea?" as I'm unsure what the limit of gh pr create should be able to accomplish by itself.

  5. BagToad commented on Nov 10, 2025

    @BagToad
    Member

    Marking this as triaged since @babakks and I think this is a good feature to investigate more deeply.

  6. added
    help wanted candidateIssue may be marked as help-wanted but not yet ready to accept PR
    on Nov 10, 2025
  7. 1 remaining item

  8. BagToad commented on Jan 30, 2026

    @BagToad
    Member

    @babakks oof good catch.

    I would vote for option 1 - omit template option. I'd like to hear from people that they want the template specifically before we make a fuss over the problems that adding it would cause.

  9. babakks commented on Feb 4, 2026

    @babakks
    Member

    Fair enough. Here's the A/C for this issue.

    Acceptance Criteria

    Interactive flow

    Given I have a branch checked out and have it pushed to the remote repo
    When I run pr create --json id,url,number, follow the prompts (interactive flow)
    Then A PR is created, and I see a JSON on stdout with the url, id and number fields of the created PR

    Non-interactive flow

    Given I have a branch checked out and have it pushed to the remote repo
    When I run pr create --json id,url,number --title foo --body bar 2>/dev/null | cat (non-interactive flow)
    Then I see a JSON on stdout with the url, id and number fields of the created PR

    JQ support

    Given I have a branch checked out and have it pushed to the remote repo
    When I run pr create --json id,url,number --jq .number --title foo --body bar 2>/dev/null | cat
    Then I see the PR number is printed on stdout

    Incompatible with --dry-run

    When I run pr create --json id --dry-run
    Then I get an error message saying these options are mutually exclusive

    Independent of --template

    Given My repo has a PR template named pull_request_template.md
    When I run pr create --json id,url,number --template pull_request_template.md
    Then I see a JSON on stdout with the url, id and number fields of the created PR, AND the PR is created off of the given template

  10. babakks commented on Feb 4, 2026

    @babakks
    Member

    This is for anyone who's going to work on this. The above comment lists the acceptance criteria that the PR must satisfy.

    Here are the places that we need to modify/improve. I didn't cover the test files, but any new/changed behaviour must accompany relevant tests.

    1. api/queries_pr.go

    Add the number field to the mutation in the api.CreatePullRequest function:

    cli/api/queries_pr.go

    Lines 531 to 539 in af4dad0

    2. pkg/cmdutil/json_flags.go

    A new helper to add --json and --jq options

    We need to create a new helper function to the cmdutil package that adds the two --json and --jq option to the given Cobra command instance. For now, it shouldn't also add the -q shorthand for --jq. This can be used as an example:

    func AddJSONFlagsWithoutShorthand(cmd *cobra.Command, exportTarget *Exporter, fields []string) {

    Make JSON flag validation aware of missing --template

    Given a hydrated Cobra command, the checkJSONFlags function in cmdutil validates the combination of JSON-related options. Currently, it assumes the --template option is always part of the JSON options group. This needs to be changed, because in our case of pr create the --template option is not part of the JSON options. Basically, we need to modify the implementation to trickle down the exclusion/inclusion of the --template option to checkJSONFlags.

    3. pkg/cmd/pr/create/create.go

    Add the JSON-related options

    We should use the newly created function to add --json and --jq options to pr create. As an example, look at the pr view command's way of doing this (below). However, since we're not going to support all api.PullRequest fields, the fields argument should be replaced with a local slice, including id, url, and number.

    cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields)

    The final JSON export should happen at the end of the submitPR function:

    func submitPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataState, projectV1Support gh.ProjectsV1Support) error {

    Note

    The api.PullRequest type already implements everything needed to export a nice JSON. So, you wouldn't need to reimplement the JSON marshalling override.

    Bail out if --json is used with --dry-run

    These options cannot be used together, so we should add a validation check in the cmd.RunE assignment, around here:

  11. added and removed
    help wanted candidateIssue may be marked as help-wanted but not yet ready to accept PR
    coreThis issue is not accepting PRs from outside contributors
    discussFeature changes that require discussion primarily among the GitHub CLI team
    on Feb 4, 2026
  12. babakks commented on Feb 10, 2026

    @babakks
    Member

    @yuvrajangadsingh, thanks for your interest, but there's already a PR for this waiting for review (See #12622).

  13. yuvrajangadsingh commented on Feb 10, 2026

    @yuvrajangadsingh
    Contributor

    @babakks Ah, didn't notice #12622 — thanks for the heads up! I'll back off on this one.

  14. added a commit that references this issue on Feb 20, 2026
    9d38b5d
  15. added a commit that references this issue on Jun 20, 2026
    327fcc6
  16. natsu0529 commented on Jul 6, 2026

    @natsu0529

    Hi! I noticed #12622 was closed. I’d like to pick this up and open a fresh PR for #11247, following the acceptance criteria above. Is that still welcome?

  17. added a commit that references this issue on Jul 18, 2026
    d88a32e
  18. LeonxLJX commented on Aug 27, 2026

    @LeonxLJX

    Hi! I'd like to work on this issue. Please let me know if I can be assigned. Thanks!

  19. LeonxLJX commented on Sep 1, 2026

    @LeonxLJX

    Hi! I'd like to add --json and --jq to gh pr create. Plan: implement the flags, add tests. May I be assigned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-prrelating to the gh pr commandhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions