JMESPath filter with dynamic variable

Describe the problem/error/question

I am trying to use the variable $itemIndex within a JMESPath filter but it does not work. The variable is not substituted.

Here’s my expression:

{{ $json.data.insertId || $jmespath($input.all(), `[?pairedItem.item < '${itemIndex}' && json.data.insertId > '0'].json.data.insertId`).last() }}

Please share your workflow

Share the output returned by the last node

The output is the following:

[
  {
    "insert_id": 60
  },
  {
    "insert_id": null
  },
  {
    "insert_id": 61
  },
  {
    "insert_id": null
  },
  {
    "insert_id": null
  }
]

While I am expecting:

[
  {
    "insert_id": 60
  },
  {
    "insert_id": 60
  },
  {
    "insert_id": 61
  },
  {
    "insert_id": 61
  },
  {
    "insert_id": 61
  }
]

Information on your n8n setup

  • n8n version: 1.32.2
  • Running n8n via: n8n Cloud

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hello @jburns

itemIndex has $ in the beginning:

{{ $json.data.insertId || $jmespath($input.all(), `[?pairedItem.item < '${$itemIndex}' && json.data.insertId > '0'].json.data.insertId`).last() }}
2 Likes

Thanks @barn4k, it works now!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.