powershell解析Json优化结果

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

当我使用REST API获取结果时,在获取返回的部分body后,我得到了多个item,我必须循环item下的值:
json ChangesInfoJson的返回值是:

[
    {
        "item":  {
                     "objectId":  "dd6d17aa11bexxxx110aeedb",
                     "originalObjectId":  "98a11c2fe8xxxx33a089fc62e986",
                     "gitObjectType":  "blob",
                     "commitId":  "a73b19232650xxxx1b6d4a6c969",
                     "path":  "/pipeline/PL_xxxx_START.json",
                     "url":  "https://dev.azure.com/xxxx"
                 },
        "changeType":  "edit"
    },
    {
        "item":  {
                     "objectId":  "13e6685cdxxxx485968d9e159",
                     "originalObjectId":  "48d0cf8xxxx19a43064",
                     "gitObjectType":  "blob",
                     "commitId":  "a73b19232650fxxx4a6c969",
                     "path":  "/trigger/TRIGGER_xxxxx_WKLY.json",
                     "url":  "https://dev.azure.com/xxxx"
                 },
        "changeType":  "edit"
    }
]

然后我使用下面的代码来解析$ChangesInfoJson

$ChangesInfoJson | foreach {
   $_.psobject.properties | foreach {
      $_.value | foreach {
            $_.psobject.properties | foreach {
               if ($_.name -eq "path"){
                    write-host change file path is:$_.value
               }   
            }
      }
   }
}

但结果是:
“更改文件路径为:string path=/pipeline/PL_xxxx。json.value
我想要的是改变文件路径是:/pipeline/PL_xxxx。json
我如何更改代码?请帮帮我

0md85ypi

0md85ypi1#

不要为同一个(或至少是非常相似的)问题提出几个问题,而是尝试理解(已经存在的)答案。对具体问题作出结论的一个好做法是:

(see how to ask
至于有赏金的问题,powershell parse Json file with \ and wildcard in the key name,只有从其他人那里得到upvote or accepted the answers才是公平的,而不是你自己的答案(这只涵盖了问题的一小部分)。

针对此问题

(我怀疑这里有一个bug。)
为此,我从您的问题powershell parse the Json after ConvertTo-Json中获取了完整的JSON示例

$Json = @' ...
{
  "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": "My new value for: TheValueIwant"
              }
            },
            "method": "post",
            "path": "/v2/SharedMailbox/Mail"
          },
          "runAfter": {},
          "type": "ApiConnection"
        }
      },
      "contentVersion": "1.0.0.0",
      "outputs": {}
    },
    "kind": "Stateful"
  }
}
  }
}
'@
$ChangeInfo = ConvertFrom-Json $Json

一旦从json转换,使用ConvertFrom-Json或通过使用Invoke-RestMethod暗示,结果是一个完全独立于(和无关)Json的PowerShell 对象。参见this answer from mklement0

访问

通常,Member-Access enumeration为您提供了一种方便的方法来获取相关值:

$ChangeInfo.changes.item.path

但是在这个特定的例子中,出现了一个bug,其中特定的成员名item显然在内部用于迭代。参见:Can't enumerate item member #19570
换句话说,您将需要“手动”枚举此特定成员:

$CommitInfo.changes.foreach{ $_.item }.path
/pipeline
/pipeline/PL_xxxx_START.json
/trigger
/trigger/TRIGGER_xxxx_WKLY.json

过滤

这意味着如果你想要E。g过滤特定路径上的urlitem,您需要执行以下操作:

$CommitInfo.changes.foreach{ $_.item }.where{ $_.path -eq '/pipeline/PL_xxxx_START.json' }.url
https://dev.azure.com/xxxx

如果你有多个路径要过滤,你可以利用这个常见的比较功能:
当输入是一个集合时,该运算符返回集合中与表达式右侧值匹配的元素。

$PathFilter =
    '/pipeline/PL_xxxx_START.json',
    '/trigger/TRIGGER_xxxx_WKLY.json'
$CommitInfo.changes.foreach{ $_.item }.where{ $PathFilter -eq $_.path }.objectId
dd6d17aa11bexxxxx10aeedb
13e6685cddxxxxd9e159

变更

要更改此相关item属性中的特定path,e.例如,第三个(数组是基于零的):

$CommitInfo.changes.foreach{ $_.item }[2].path = 'Another path'
$CommitInfo |ConvertTo-Json -Depth 10

结果:

{
  "changeCounts": {
"Edit": 4
  },
  "changes": [
{
  "item": {
    "objectId": "7e852adxxxxxxxba3792",
    "originalObjectId": "c77692938423cxxxx912bf7",
    "gitObjectType": "tree",
    "commitId": "a73b19232650xxxxx1b6d4a6c969",
    "path": "/pipeline",
    "isFolder": true,
    "url": "https://dev.azure.com/xxxx"
  },
  "changeType": "edit"
},
{
  "item": {
    "objectId": "dd6d17aa11bexxxxx10aeedb",
    "originalObjectId": "98a11c2fe819xxxx089fc62e986",
    "gitObjectType": "blob",
    "commitId": "a73b19232650fcxxxxx6d4a6c969",
    "path": "/pipeline/PL_xxxx_START.json",
    "url": "https://dev.azure.com/xxxx"
  },
  "changeType": "edit"
},
{
  "item": {
    "objectId": "7b3cfd336cxxxxed652446",
    "originalObjectId": "a67b91d65xxxx9c4605143",
    "gitObjectType": "tree",
    "commitId": "a73b19232650fcbxxxx4a6c969",
    "path": "Another path",
    "isFolder": true,
    "url": "https://dev.azure.com/xxxx"
  },
  "changeType": "edit"
},
{
  "item": {
    "objectId": "13e6685cddxxxxd9e159",
    "originalObjectId": "48d0cf833xxxx43064",
    "gitObjectType": "blob",
    "commitId": "a73b19232xxxx6d4a6c969",
    "path": "/trigger/TRIGGER_xxxx_WKLY.json",
    "url": "https://dev.azure.com/xxxx"
  },
  "changeType": "edit"
}
  ]
}

相关问题