powershell解析Json文件,在密钥名称中使用通配符

x7yiwoj4  于 2023-05-02  发布在  Shell
关注(0)|答案(1)|浏览(168)

我使用powershell循环通过REST API获得的多个Json文件。但难点在于有些节点并不固定,它们在不同的json体中返回的结果是不同的,通配符似乎没有任何作用。
我的测试Json文件Workflow_Info如下:

{
   "id": "/subscriptions/fcxxxx7/resourceGroups/xxxxxx/providers/Microsoft.Web/sites/xxxx/workflows/Test_email",
   "name": "xxxxxxxxx/Test_email",
   "type": "Microsoft.Web/sites/workflows",
   "kind": "Stateful",
   "location": "East Asia",
   "properties": {
      "files": {
         "Test_email/workflow.json": {
            "definition": {
               "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
               "actions": {
                  "Send_an_email_from_a_shared_mailbox_(V2)": {
                     "inputs": {
                        "host": {
                           "connection": {
                              "referenceName": "TheValueIwant"
                           }
                        },
                        "method": "post",
                        "path": "/v2/SharedMailbox/Mail"
                     },
                     "runAfter": {},
                     "type": "ApiConnection"
                  }
               },
               "contentVersion": "1.0.0.0",
               "outputs": {}
            },
            "kind": "Stateful"
         }
      }
   }
}

另一个Json body是:

{
   "id": "/subscriptions/xxx/resourceGroups/xxxx/providers/Microsoft.Web/sites/xxx/workflows/test_email",
   "name": "xxx/test_email",
   "type": "Microsoft.Web/sites/workflows",
   "kind": "Stateful",
   "location": "East Asia",
   "properties": {
      "files": {
         "Test_email/workflow.json": {
            "definition": {
               "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
               "actions": {
                  "For_each": {
                     "actions": {},
                     "foreach": "@triggerBody()?['attachments']",
                     "runAfter": {},
                     "type": "Foreach"
                  }
               },
               "contentVersion": "1.0.0.1",
               "outputs": {},
               "triggers": {
                  "When_a_new_email_arrives_in_a_shared_mailbox_(V2)": {
                     "inputs": {
                        "host": {
                           "connection": {
                              "referenceName": "office365"
                           }
                        },
                        "method": "get"
                     },
                     "recurrence": {},
                     "splitOn": "@triggerBody()?['value']",
                     "type": "ApiConnection"
                  }
               }
            },
            "kind": "Stateful"
         }
      },
      "flowState": "Disabled",
      "health": {
         "state": "Healthy"
      }
   }
}

我想获取的是节点referenceName的值。

但是节点在不同的json中有不同的值,对于第一个Json,我可以通过下面的方法得到它:

$referenceName= $Workflow_Info.properties.files."Test_email/workflow.json".definition.actions.Send_an_email_from_a_shared_mailbox_(V2).inputs.host.connection.referenceName

但是对于第二个Json,我必须将节点更改为:

$referenceName= $Workflow_Info.properties.files."Test_email/workflow.json".definition.triggers.When_a_new_email_arrives_in_a_shared_mailbox_(V2).inputs.host.connection.referenceName

其中2个节点不同:一个是**.actions.Send_an_email_from_a_shared_mailbox_(V2).,另一个是.triggers.When_a_new_email_arrives_in_a_shared_mailbox_(V2).**。

**如何使用通用语法获取最终节点?**我无法为每个json文件单独指定节点路径,因为我有数百个json文件。

我尝试使用通配符*,但不工作。

$referenceName= $Workflow_Info.properties.files."Test_email/workflow.json".definition.actions.'*'.inputs.host.connection.referenceName
w8biq8rn

w8biq8rn1#

假设您要查找的属性的名称始终为connection,并且它始终嵌套在definition属性中,则以下方法应该适用。使用正在讨论的示例,并假设它们存储在$json1$json2中,结果将是TheValueIwantoffice365
从理论上讲,如果Jsons总是具有相同的结构,那么您可以将$json而不是$json.properties.files.'Test_email/workflow.json'.definition入队,它应该会找到您正在寻找的值,但值得注意的是,此代码并没有处理可能的数组。

$queue = [System.Collections.Generic.Queue[object]]::new()
# enqueue the definition property of the json
$queue.Enqueue($json.properties.files.'Test_email/workflow.json'.definition)

while($queue.Count) {
    $item = $queue.Dequeue()
    # enumerate all properties on this object
    foreach($property in $item.PSObject.Properties) {
        # if this property name is equal to 'connection'
        if($property.Name -eq 'connection') {
            # and the value of this property is an object with
            # a property name 'referenceName'
            if($value = $property.Value.referenceName) {
                # then output this value
                $value
                # and break this loop, no need to keep checking
                break
            }
        }
        # else, enqueue the value of this property
        $queue.Enqueue($property.Value)
    }
}

相关问题