PowerShell从Json对象中有条件地获取特定值

fcg9iug3  于 2023-04-13  发布在  Shell
关注(0)|答案(1)|浏览(128)

我使用REST API获取返回值,将返回值转换为json后,我想从json对象中获取我想要的值,但仍然无法实现。
我的JSON内容GetChangesInfo喜欢:

{
    "changeCounts":  {
                         "Edit":  1,
                         "Add":  1
                     },
    "changes":  [
                    {
                        "item":  {
                                     "objectId":  "2389ac80XXXXXXXXXXXX7c2c3455c",
                                     "originalObjectId":  "fe55989f9XXXXXXXXXXXX0817d204cf8",
                                     "gitObjectType":  "tree",
                                     "commitId":  "9e8bc9e78542d70XXXXXXXXXXa7827356a7",
                                     "path":  "/trigger",
                                     "isFolder":  true,
                                     "url":  "https://dev.azure.com/XXXX/XXXXXXXXXXXXXXXXX2086/_apis/git/repositories/8e2f799XXXXXXXXXXXXXXXb627-8f7357e1c559/items/trigger?versionType=Commit\u002XXXXXXXX45ea0a7827356a7"
                                 },
                        "changeType":  "edit"
                    },
                    {
                        "item":  {
                                     "objectId":  "ecd29dbb6169XXXXXXXXXXX30ed7a44e9eb72",
                                     "gitObjectType":  "blob",
                                     "commitId":  "9e8bc9e78542dXXXXXXXXXXX27356a7",
                                     "path":  "/trigger/TRIGGER_Test.json",
                                     "url":  "https://dev.azure.com/XXXX/82aXXXXXXXXXXXXXf402086/_apis/git/repositories/8e2f7999-dccXXXXXXXXXXXXXXe1c559/items/trigger%2FTRIGGER_Test.json?versionType=Commit\u0026veXXXXXXXXXXXXXX0545ea0a7827356a7"
                                 },
                        "changeType":  "add"
                    }
                ]
}

我想得到的是"path": "/trigger/TRIGGER_Test.json"的值,而不是"path": "/trigger"
因此,我使用以下代码和过滤器Where-Object {($_.item -notcontains 'isFolder')}来获取值:

$GetChanges = "https://dev.azure.com/xxx/xxx/_apis/git/repositories/xxx/commits/xxx/changes?api-version=7.1-preview.1"

$GetChangesInfo = (Invoke-RestMethod -Uri $GetChanges -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

$GetChangesInfo = $($GetChangesInfo | ConvertTo-Json -Depth 100)

$darts = $GetChangesInfo.changes | Where-Object {($_.item -notcontains 'isFolder')}

Write-Host "This is_$darts"

因为changes包含相同的节点itempath。但是我只需要包含特定文件的文件名,而不是文件夹
因此,我根据item下是否包含isFolder节点将其用作过滤器。
但是我不知道它的价值。请帮帮忙。
结果:

This is_

更新:

现在,我可以得到所有的路径,比如:

/DB/main/StoreProcedure/DB_Test.sql
/trigger/TRIGGER_Test.json
/notebooks/HKAZEDDB0002/BASIS/NoteBook_Test.py
/pipeline/PL_Test2.json
/pipeline/PL_Test.json

我想过滤所有上述路径不包括文件夹/main,第一个路径值。

wko9yo5t

wko9yo5t1#

如果我没弄错的话,这就足够了...

$GetChangesInfo = @'
{
    "changeCounts": {
        "Edit": 1,
        "Add": 1
    },
    "changes": [
        {
            "item": {
                "path": "/trigger",
                "isFolder": true,
            },
            "changeType": "edit"
        },
        {
            "item": {
                "path": "/trigger/TRIGGER_Test.json",
            },
            "changeType": "add"
        }
    ]
}
'@ |
    ConvertFrom-Json

$darts = $GetChangesInfo.changes | Where-Object {-not $_.item.isFolder} 
$darts.item.path

我删除了JSON中不必要的部分。
对于路径检查...最简单的方法是使用-match-notmatch

$PathList = 
'/DB/main/StoreProcedure/DB_Test.sql',
'/trigger/TRIGGER_Test.json',
'/notebooks/HKAZEDDB0002/BASIS/NoteBook_Test.py',
'/pipeline/PL_Test2.json',
'/pipeline/PL_Test.json'

$PathList |
    Where-Object {$_ -notmatch '/main'}

相关问题