Simple Conditional Variable Value Setter Based On JSON Node Existence

Describe the problem/error/question

  1. Workflow receives a webhook from Shopify that a new order is placed.
  2. Workflow also has a manual execution node for testing.
  3. The first node after the Shopify new order webhook is hit (or the workflow is manually executed via the manual node), is a Set node.
  4. In this Set node, I have a variable called OrderID

Here’s the money maker:

I am trying to do a simple expression in the value setter for OrderID that checks to see if the order-id node exists in the JSON payload from the Shopify webhook. If it does, then I want to set the OrderID variable value to Shopify webhook JSON → order id node value.

If the Shopify webhook JSON → order ID node doesn’t exist, then I want to set the OrderID value to a static order ID so that I can automatically test the workflow without having to change variable values.

Here is what I have for the expression in the OrderID value (one line):

{{ $(‘Shopify Paid Order Webhook Received’).item.json[“body”][“id”] ? $(‘Shopify Paid Order Webhook Received’).item.json[“body”][“id”] : “5118138253462”; }}

What am I doing wrong?

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

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

Hey @chidev,

Your expression looks ok to me although you don’t need the ; at the end. Are you seeing an error when you run that?

1 Like

I got it fixed. It was a simple case of casting everything to string using toString() and then it began to work. Here’s the updated variable setter expression with conditional which may be helpful for folks in the future.

FYI, when Shopify sends a test webhook using sample order data, the order ID is 820982911946154500.

I have this expression setting the value to “5118138253442” which is the first order we ever received on Shopify from years ago. Thus it’s easy for us to see/test the workflow and know what is test data that can be removed/deleted after testing. :slight_smile:

{{ $('Shopify Paid Order Webhook Received').item.json["body"]["id"].toString() == "820982911946154500" ? "5118138253442" : $('Shopify Paid Order Webhook Received').item.json["body"]["id"].toString() }}

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