【问题标题】:How to return specific item from Cloud Workflows instance?如何从 Cloud Workflows 实例返回特定项目?
【发布时间】:2022-06-17 21:05:15
【问题描述】:

我无法从 JSON 响应实例中获取数据。我正在使用 Cloud Workflows 来获取有关我的虚拟机当前状态的信息。我正在使用 .get 函数,它返回这个高结构化的长 JSON,例如launchResult 返回为:

{
   "name":"some name",
   "status":"some status",
   "items":[
      {
         "key":"key1",
         "property1":"xxxx",
         "property2":"ccvdvdvd"
      },
      {
         "key":"key2",
         "property1":"xxxrerex",
         "property2":"ccvdveedvd"
      }
   ],
   "kind":"some kind"
}

我可以通过${launchResult.status}返回例如“一些状态”, 甚至key1,如{launchResult.items[0].key}

问题是:我怎样才能像launchResult.items["key" == "key1"].property1那样做某事?我的意思是我想根据密钥从项目中返回property1

【问题讨论】:

  • 你有机会看看我的answer吗?

标签: json yaml conditional-statements google-workflows


【解决方案1】:

要根据地图的另一个属性的值从地图列表中的项目中获取属性,我建议如下:

  • 遍历列表的元素,直到获得具有所需属性的元素。

这是我为实现此目的而在Cloud Workflows 中显示how to use iterationsconditionals 的示例代码:

main:
  params: []
  steps:
  - define:
      assign:
        # Incremental variable set to zero
        - i: 0
        # Value to look up in list
        - cond: "key2"
        # A variable that contains a sample data as the JSON
        # showed in the question
        - test: 
            name: some name
            status: some status
            items:
            - key: "key1"
              property1: xxxx
              property2: ccvdvdvd
            - key: "key2"
              property1: xxxrerex
              property2: ccvdveedvd
            - key: "key3"
              property1: xxex
              property2: ccvdvffdvd
            kind: some kind
  - lookupInItems:
      switch:
        # Here we're looking for the element over the list test.items until the condition is met 
        # or the list ends
        - condition: ${i < len(test.items) and test.items[i].key!=cond}
        # If the condition is not met, keep iterating using iterate step
          next: iterate
      # If the element is found or the list ends, jumps to the read step
      next: read
  - iterate:
      assign: 
      # Increment the increment variable i
        - i: ${i+1}
      next: lookupInItems
  # If the iteration ends, check for the value
  - read:
      switch:
        # Check if the value was found
        # if this check is not made, an out of index error will occur when trying to show
        # the property of a value does not exists
        - condition: ${i < len(test.items)}
          next: found
      next: notfound
  # Print a message on the sys.log either the value was found or not
  - notfound:
      call: sys.log
      args:
        text: ${"Value " + cond + " not found in list"}
        severity: "WARNING"
      next: end
  - found:
      call: sys.log
      args:
        text: ${test.items[i].property1}
        severity: "INFO"
      next: end

另见:

【讨论】:

    【解决方案2】:

    请注意,for 循环会使这更容易:

    https://cloud.google.com/workflows/docs/reference/syntax/iteration

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多