Conditional expression in SnapLogic

3.7k Views Asked by At

I need to check whether or not an entry is present in the data output from a REST call. The JSON output looks something like this:

{
  "entity":  {
    "entries":[
      {
        "ID": "1",
        "Pipeline": "Pipeline_1",
        "State":"Completed"
      }
    ],
  "duration":1074,
  "create_time":"2010-10-10"
  }
}

I want to check if for example, Pipeline_1 is missing, then I want the pipeline to print out that 'Pipeline_1 is missing', if not - null. I have tried using the ternary (?) expression:

!$Pipeline.contains ("Pipeline_1") ? "Pipeline_1 is missing" : null && !$Pipeline.contains ("Pipeline_2") ? "Pipeline_2 is missing" : null

I'm having problems with the syntax and I just can't get it right using this method, because it only processes the first query.

I have also tried using the match method, but haven't had success with it either:

match $Pipeline {
    $Pipeline!=("Pipeline_1") => 'Pipeline_1 is missing',
    $Pipeline!=("Pipeline_2") => 'Pipeline_2 is missing',
    _ => 'All of the pipelines have been executed successfully'
}

I have to check for multiple conditions. Any suggestions on how I should nest the conditional expressions? Thank you in advance.

1

There are 1 best solutions below

0
On

Assuming that you are not splitting the array $entity.entries[*] and processing the incoming document as is, following is a possible solution.

Test Pipeline:

test pipeline

Input:

{
    "entity": {
        "entries": [
            {
                "ID": "1",
                "Pipeline": "Pipeline_1",
                "State": "Completed"
            }
        ],
        "duration": 1074,
        "create_time": "2010-10-10"
    }
}

Expression:

{
    "Pipeline_1": $entity.entries.reduce((a, c) => c.Pipeline == "Pipeline_1" || a, false),
    "Pipeline_2": $entity.entries.reduce((a, c) => c.Pipeline == "Pipeline_2" || a, false)
}.values().reduce((a, c) => c && a, true) ? "All pipelines executed successfully" : "Pipeline(s) missing"

Output:

enter image description here


If you don't want to do it in a single expression, then you can use a Conditional snap like as follows.

enter image description here

Following is the output of the Conditional snap.

enter image description here

Then you can process it as you please.