iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔄

Processing Arrays with Loops in SORACOM Flux

に公開

What I want to do

I will share some tips for processing arrays using loops in SORACOM Flux.
In a previous blog post, I mentioned that it is difficult to handle arrays of unknown length in SORACOM Flux, but I found that this can be solved by using loops.

How to do it

Let's configure SORACOM Flux.
If you are wondering "What is SORACOM Flux?", please check here.

Configuration Overview

alt text
We will create a flow that performs the following.
The numbers correspond to the balloon numbers in the diagram above explaining the flow.

  1. Trigger settings
    Event source: API/Manual Execution
    Output channel: API Channel
    Since we are confirming the loop functionality this time, we select API/Manual Execution.
    In actual applications, please select an appropriate trigger.

  2. Simple Object Detection Action
    Input channel: API Channel

    Configuration of the API Action block

    Settings

    Major Item Detailed Item Setting Value Remarks
    CONDITION Action execution condition (Blank)
    CONFIG Image URL https://blog.soracom.com/ja-jp/wp-content/uploads/2024/07/soracam-team-1-1024x683.jpg Image borrowed from SORACOM official blog
    OUTPUT Send action output to another channel Enabled
    OUTPUT Destination channel Output Channel name is arbitrary

    Configuration screen (example)
    alt text

    Output JSON

    {
      "person": 6,
      "car": 0,
      "bus": 0,
      "truck": 0,
      "cat": 0,
      "dog": 0,
      "objects": [
        {
          "location": [
            0.6609779,
            0.17652985,
            0.9373262,
            0.9943735
          ],
          "score": 0.8904824,
          "label": "person",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.37141305,
            0.3185921,
            0.5251937,
            0.969292
          ],
          "score": 0.8751769,
          "label": "person",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.04004524,
            0.21790065,
            0.21095791,
            0.84099317
          ],
          "score": 0.8547369,
          "label": "person",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.4620108,
            0.37004647,
            0.6945141,
            0.9970469
          ],
          "score": 0.83913076,
          "label": "person",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.23511018,
            0.3791213,
            0.39225024,
            0.8577786
          ],
          "score": 0.8179504,
          "label": "person",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.13871717,
            0.3871962,
            0.27425474,
            0.8432603
          ],
          "score": 0.7499229,
          "label": "person",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.4429141,
            0.47117797,
            0.48522377,
            0.6288479
          ],
          "score": 0.2793063,
          "label": "tie",
          "ts": "2025-05-07T01:48:41Z"
        },
        {
          "location": [
            0.39311492,
            0.4475629,
            0.42104053,
            0.6097326
          ],
          "score": 0.2578054,
          "label": "tie",
          "ts": "2025-05-07T01:48:41Z"
        }
      ]
    }
    

    Output channel: Output

  3. Set loop counter (num), loop limit (length), and the array to decompose

    Input channel: Output

    Configuration of the Republish block

    Input payload

    {
      "person": 6,
      "car": 0,
      "bus": 0,
      "truck": 0,
      "cat": 0,
      "dog": 0,
      "objects": [···]
    }
    

    *The content of objects is omitted.

    Settings

    Major Item Detailed Item Setting Value Remarks
    CONDITION Action execution condition (Blank)
    CONFIG Transform data ✅️
    CONFIG Content Type application/json Default setting
    CONFIG Content {000a "num" : 0,000a "length" : ${len(payload.objects)},000a "objects" : ${payload.objects}000a} Calculate the length of the array output in objects.000aInclude len() within ${}.000aIt might be tempting to write len(${payload.objects}), but that is incorrect.
    OUTPUT Send action output to another channel Enabled
    OUTPUT Destination channel Output Channel

    alt text

    Output JSON

    {
        "num":0,
        "length":8,
        "objects":[···]
    }
    

    *The content of objects is omitted.

    Output channel: Output Channel

  4. num increment and extraction of the num-th element of payload.objects
    Input channel: Output Channel

    Configuration of the Republish block
    {
        "num":0,
        "length":8,
        "objects":[···]
    }
    

    *The content of objects is omitted.

    Settings

    Major Item Detailed Item Setting Value Remarks
    CONDITION Action execution condition payload.num < payload.length - 1 Perform increment and output when the condition on the left is met.
    CONFIG Transform data ✅️
    CONFIG Content Type application/json Default setting
    CONFIG Content {
    "num":${payload.num+1},
    "length": ${payload.length},
    "objects": ${payload.objects}
    }
    Increment the input num and output.
    Write the increment +1 inside ${}.
    Output length and objects as they are.
    OUTPUT Send action output to another channel Enabled
    OUTPUT Destination channel Output Channel Loop by returning to the input channel of this action
    {
        "num":1,
        "length":8,
        "objects":[···]
    }
    

    *The content of objects is omitted.

    Output channel: Output Channel (same as the input channel for this action)

  5. Notify the num-th element
    Input channel: Output Channel

    Configuration of the Slack notification action

    Input payload

    {
        "num":0,
        // Incremented as 1, 2, 3, 4... and entered
        "length":8,
        "objects":[···]
    }
    

    Settings

    Major Item Detailed Item Setting Value Remarks
    CONDITION Action execution condition (Blank)
    CONFIG URL URL starting with https://hooks.slack.com/services Obtain the webhook URL by referring to the method here (Japanese documentation).
    CONFIG Payload ${payload.num} :
    ${payload.objects[payload.num]}
    Put the number on the 1st line and the num-th element of payload.objects on the 2nd line.
    OUTPUT Send action output to another channel Disabled

    alt text

Execution Results

Notifications are sent to SORACOM's Slack as shown below.
However, it seems the order in which notifications arrive in Slack is unordered.
Caution is needed for applications that are sensitive to the order of the array and the arrival order of requests.
alt text

Note that since the execution condition payload.num < payload.length - 1 was set for the Republish action where the loop was applied, this became the upper limit for the number of loops. However, if this condition is removed, the loop is designed to stop at 10 times.
If you want to process an array with a length of 10 or more, I think you will be able to process each element by creating another Republish action setting to connect to the Output channel in parallel, setting the initial value of num to 10, and running the loop.

{
    "num" : 10,
    "length" : ${len(payload.objects)},
    "objects": ${payload.objects}
}
GitHubで編集を提案

Discussion