Python script failing to create Jira ticket: 'project' KeyError

Hey everyone, I’m stuck with a Python script I made to create Jira tickets automatically. It’s giving me a headache!

The script runs on Ubuntu with Python 3.6 and 3.7. Here’s what it looks like:

def create_ticket_info(sprint, user_id, other_stuff):
    return {
        'fields': {
            'project': {'key': 'MAIN'},
            'summary': f'Query: {user_id}',
            'description': 'Ticket details go here',
            'issuetype': {'name': 'Issue'},
            'Epic Link': 'MAIN-100',
            'Sprint': sprint,
            'assignee': 'coolDev'
        }
    }

ticket_data = create_ticket_info(sprint, user_id, other_stuff)
jira_connection.create_issue(fields=ticket_data)

But when I run it, I get this error:

KeyError: 'project'

It’s saying it can’t find the ‘project’ key, even though I clearly included it in the dictionary. I expected it to work smoothly and file the tickets. Any ideas on what’s going wrong?

I’ve encountered a similar issue before. The problem might be in how you’re passing the ticket data to the create_issue method. Try modifying your last line to:

jira_connection.create_issue(fields=ticket_data[‘fields’])

This ensures you’re passing just the ‘fields’ dictionary, not the entire ticket_data structure. Jira’s API expects the fields directly, not nested under another key.

Also, double-check your Jira connection setup. Make sure you have the necessary permissions to create issues in the ‘MAIN’ project. Sometimes, permission issues can manifest as unexpected errors.

If this doesn’t solve it, you might want to print out ticket_data just before creating the issue to verify its structure. Hope this helps!

hey nova56, ive run into this before. make sure ur passing just the ‘fields’ part to create_issue. try this:

jira_connection.create_issue(fields=ticket_data[‘fields’])

also double check ur jira permissions. sometimes weird errors pop up if u dont have the right access. good luck!