0

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.

tee_thesee
  • 9
  • 1
  • 4
  • It is not exactly clear what final output you are expecting. – Bilesh Ganguly Dec 08 '19 at 05:28
  • Also, how are you getting `$Pipeline` in the root if it is part of an object in the `entries[]` array. Are you splitting with the expression `$entity.entries[*]` before doing your validation? – Bilesh Ganguly Dec 08 '19 at 05:31
  • If you are splitting, then how can you validate just based on one object? And if you are not splitting, neither of the shared expressions will work. – Bilesh Ganguly Dec 08 '19 at 05:34

1 Answers1

2

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.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58