How to stop pipe jq execution based on condition?

155 Views Asked by At

Using ActiveCampain work flows, I have a pipeline in which I want to execute code conditionally based on the output of a jq command.

For example, I want to execute and !http and !jq command if .status == "event-created".

JSON

{
    "id":1,
    "firstname": "Nj",
    "lastname": "Bhanushali",  
    "email": "[email protected]",  
    "title":"Call with new lead",
    "status":"event-created" 
}

Code

"!pipe": [
    {
        "!jq": ".status"
    },  
    //call this jq if .status == "event-created" 
    {
        "!http": {
            "method": "GET",
            "path": "/contact/fields"
        }
    },
    {
        "!jq": "${piped_content::1}"
    },
]
1

There are 1 best solutions below

0
On BEST ANSWER

A quick look at the docs suggest there's a !switch "command" you can use.

{
  "!switch": {
    "jq": "if .status == \"event-created\" then 0 else 1 end",
    "cases": [
      {
        "!pipe": [
          {
            "!http": {
              "method": "GET",
              "path": "/contact/fields"
            }
          },
          {
            "!jq": "${piped_content::1}"
          }
        ]
      },
      {
      }
    ]
  }
}

Notes:

  • I don't know if it's acceptable to have an empty hash for one of the cases. Maybe you have to use null? Maybe you have to omit it? If you omit it, maybe you have to use if .status == "event-created" then 0 else empty end? If that works, that would be the cleanest solution. Adjust as necessary.

  • I don't know if ${piped_content::1} is still correct with the changes I've made. Adjust as necessary.