简体   繁体   English

基于 Power Automate 流程中的子数组过滤 JSON

[英]Filtering JSON based on sub array in a Power Automate Flow

I have some json data that I would like to filter in a Power Automate Flow.我有一些 json 数据,我想在 Power Automate Flow 中过滤这些数据。

A simplified version of the json is as follows: json的简化版本如下:

[
  {
    "ItemId": "1",
    "Blah": "test1",
    "CustomFieldArray": [
      {
        "Name": "Code",
        "Value": "A"
      },
      {
        "Name": "Category",
        "Value": "Test"
      }
    ]
  },
  {
    "ItemId": "2",
    "Blah": "test2",
    "CustomFieldArray": [
      {
        "Name": "Code",
        "Value": "B"
      },
      {
        "Name": "Category",
        "Value": "Test"
      }
    ]
  }
]

For example, I wish to filter items based on Name = "Code" and Value = "A".例如,我希望根据 Name = "Code" 和 Value = "A" 过滤项目。 I should be left with the item with ItemId 1 in that case.在这种情况下,我应该只剩下 ItemId 为 1 的项目。

I can't figure out how to do this in Power Automate.我不知道如何在 Power Automate 中执行此操作。 It would be nice to change the data structure, but this is the way the data is, and I'm trying to work out if this is possible in Power Automate without changing the data itself.更改数据结构会很好,但这就是数据的方式,我正在尝试在不更改数据本身的情况下在 Power Automate 中实现这一点。

Firstly, I had to fix your JSON, it wasn't complete.首先,我必须修复你的 JSON,它还不完整。

Secondly, filtering on sub array information isn't what I'd call easy.其次,过滤子阵列信息并不容易。 However, to get around the limitations, you can perform a bit of trickery.但是,要绕过这些限制,您可以使用一些技巧。

过滤阵列

Prior to the step above, I create a variable of type Array and called it Array .在上述步骤之前,我创建了一个Array类型的变量并将其命名为Array

In the step above, the left hand side expression is...在上面的步骤中,左侧表达式是...

string(item()?['CustomFieldArray'])

... and the contains comparison on the right hand side is simply as you can see, a string with the appropriate filter value... ...右侧的contains比较就像您所看到的那样,是一个具有适当过滤值的字符串...

{"Name":"Code","Value":"A"}

... it's not an expression or a proper object, just a string. ...它不是表达式或正确的 object,只是一个字符串。

If you need to enhance it to cater for case sensitive values, just set everything to lower case using the toLower expression on the left.如果您需要增强它以满足区分大小写的值,只需使用左侧的toLower表达式将所有内容设置为小写即可。

Although it's hard to see, that will produce your desired result...虽然很难看到,但这会产生你想要的结果......

结果

... you can see by the vertical scrollbars that it's reduced the size of the array. ...您可以通过垂直滚动条看到它减小了数组的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM