Jira Expression API Call

I am working on a project to create some reporting on user’s hours logged and the projects(issues) that they are logged to. We are needing a number of custom fields returned to get the appropriate issue details and worklog information. My call is to the /rest/api/3/expression/evaluate with the following call. My return is always a 400 error “There was an error parsing JSON. Check that your request body is valid.”. I’ve tried a number of different variations on the call but I cannot seem to get it to return data. Any help would be appreciated.

{
           "expression": 
             issues.filter(issue => 
               projects.includes(issue.project) 
               worklogDateRange.includes(issue.worklog.startDate)
               userList.includes(issue.worklog.author)
             ).map(issue => ({
               worklogEntries: issue.worklog.map(work => ({
                 timeWorked: work.timeSpentSeconds,
                 dateEntered: work.started,
                 comments: work.comment
               })),
               fixVersion: issue.fields.customfield_10044,
               customFields: {
                 customfield_10050: issue.fields.customfield_10050,
                 customfield_10049: issue.fields.customfield_10049,
                 customfield_10041: issue.fields.customfield_10041,
                 customfield_10042: issue.fields.customfield_10042,
                 customfield_10043: issue.fields.customfield_10043,
                 customfield_10048: issue.fields.customfield_10048
               },
               status: issue.fields.status,
               customfield_10094: issue.fields.customfield_10094,
               timespentseconds: issue.timetracking.timeSpentSeconds,
               summary: issue.fields.summary
             }))
           ,
           "context": {
             "projects": ["[project1]", "[project2]"],
             "worklogDateRange": ["2025-02-01", "2025-02-02"],
             "userList": ["[User1]", "[User2]"]
           }
         }

Hi @BartTitus
You’re missing the double quotes around the Jira expression, which is passed as a JSON string.

Hi @david2,

Modifying it to contain the quotes did not seem to help. Formatting was definitely wrong but it seems like something else is incorrect in the field results.

{
“expression”:
"issues.filter(issue =>
projects.includes(issue.project)
worklogDateRange.includes(issue.worklog.startDate)
userList.includes(issue.worklog.author)
).map(issue => ({
worklogEntries: issue.worklog.map(work => ({
timeWorked: work.timeSpentSeconds,
dateEntered: work.started,
comments: work.comment
})),
fixVersion: issue.fields.customfield_10044,
customFields: {
customfield_10050: issue.fields.customfield_10050,
customfield_10049: issue.fields.customfield_10049,
customfield_10041: issue.fields.customfield_10041,
customfield_10042: issue.fields.customfield_10042,
customfield_10043: issue.fields.customfield_10043,
customfield_10048: issue.fields.customfield_10048
},
status: issue.fields.status,
customfield_10094: issue.fields.customfield_10094,
timespentseconds: issue.timetracking.timeSpentSeconds,
summary: issue.fields.summary
}))"
,
“context”: {
“projects”: [“[project1]”, “[project2]”],
“worklogDateRange”: [“2025-02-01”, “2025-02-02”],
“userList”: [“[User1]”, “[User2]”]
}
}

Hello @BartTitus

Irrespective of the expression being valid or not, the JSON request body itself is invalid, hence the error message you are getting.

  1. Strings in JSON cannot use physical line breaks, so you must concatenate the expression string object into one long, continuos string. You cannot use JSON-like-formatting inside a string object.
  2. Your JSON uses left and right quotation marks (“ and ”) as object delimiters. That too is not valid JSON. You must use double inverted commas (") to delimit objects.

Use a JSON linting tool to check your request bodies are correctly formatted before submitting them to a REST API endpoint

Hi @BartTitus
You must first make sure your JSON payload is correct and you don’t get the same error back. Then you’ll need to correct the Jira expression that is currently incorrect. For example, in the filter expression, you should use && to combine the 3 tests. There are other problems too. For example, accessing fields of the issue does not use “.fields” like it does in JMWE’s Nunjucks scripts but something like issue.customfield_12345

1 Like

Hi All,

I have reworked the query here, but it seems like my request variables or syntax are incorrect. Any help would be appreciated.

{
  "expressions": "issues.filter(issue => projects.includes(issue.project) && worklogDateRange.includes(issue.worklog.startDate) && userList.includes(issue.worklog.author)).map(issue => ({ worklogEntries: issue.worklog.map(work => ({ timeWorked: work.timeSpentSeconds, dateEntered: work.Created, comments: work.id, Author: work.author })), fixVersion: issue.customfield_10044, customFields: { customfield_10050: issue.customfield_10050, customfield_10049: issue.customfield_10049, customfield_10041: issue.customfield_10041, customfield_10042: issue.customfield_10042, customfield_10043: issue.customfield_10043, customfield_10048: issue.customfield_10048 }, status: issue.status.name, customfield_10094: issue.customfield_10094, timespentseconds: issue.timetracking.timeSpentSeconds, summary: issue.summary }))",
  "contextVariables": {
    "projects": ["Project1","Project2"],
    "worklogDateRange": ["2025-02-01", "2025-02-02"],
    "userList": ["User 1", "User 2"]
  }
}

@BartTitus I noticed a few mistakes in the payload:

  • You’ve referenced attributes as expressions, whereas an expression is expected.
  • The project field should be an object, but an array has been passed instead.
  • The userlist should be provided within a custom object, including their respective accountId and type as user.
  • The worklogDateRange is not an available context variable.

I highly recommend referring to the official documentation here while constructing the payload.

1 Like

thanks for the help!

Hi,

I work with Bart on the same team, and in looking into this, this is now our modified query:

{
  "expression": "issues.filter(issue => projects.includes(issue.project.key) && issue.worklog.some(work => new Date(work.started).getTime() >= new Date(startDate).getTime() && new Date(work.started).getTime() <= new Date(endDate).getTime() && userList.some(user => user.accountId == work.author.accountId))).map(issue => ({ worklogEntries: issue.worklog.filter(work => new Date(work.started).getTime() >= new Date(startDate).getTime() && new Date(work.started).getTime() <= new Date(endDate).getTime() && userList.some(user => user.accountId == work.author.accountId)).map(work => ({ timeWorked: work.timeSpentSeconds, dateEntered: work.created, comments: work.id, Author: work.author })), fixVersion: issue.customfield_10044, customFields: { customfield_10050: issue.customfield_10050, customfield_10049: issue.customfield_10049, customfield_10041: issue.customfield_10041, customfield_10042: issue.customfield_10042, customfield_10043: issue.customfield_10043, customfield_10048: issue.customfield_10048 }, status: issue.status.name, customfield_10094: issue.customfield_10094, timespentseconds: issue.timetracking ? issue.timetracking.timeSpentSeconds : null, summary: issue.summary }))",
  "contextVariables": {
    "project": {
      "key": "COS"
    },
    "startDate": "2025-02-01",
    "endDate": "2025-02-02",
    "custom": {
      "userList": [
        {
          "accountId": "XXX",
          "type": "user"
        },
        {
          "accountId": "XXX",
          "type": "user"
        },
        {
          "accountId": "XXX",
          "type": "user"
        }
      ]
    }
  }
}

We are still seeing the same issue as above (the bad request), is there anything else we are missing? I tried to edit it to match the feedback above.

To be honest @TonyCharles , you, Bart, and your entire ‘team’ seem to be just randomly guessing how to use the Evaluate Jira expression using enhanced search API endpoint and how to construct Jira expressions that work in a particular context.

  1. The request is invalid because the endpoint has no such request parameter called contextVariables, it only has a parameter called context.
  2. The expression is invalid because there is no such context variable called projects (plural), only project (singular).
  3. The expression is invalid for that declared context, because it uses an issues identifier which isn’t defined.

Use a REST API test tool FIRST to debug your API requests. Break the problem down into small chunks; start with a basic request that works, gradually add more functionality and find at which point things go wrong, then critically compare what you changed with what you find when you… read the documentation.

:roll_eyes: