如何在PowerShell中访问嵌套的JSON字段

z6psavjg  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(245)

我正在尝试访问Azure Advisor使用PowerShell创建的JSON对象中的特定字段。我有以下对象:

@{id=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxxx;
category=Cost;
impact=High;
impactedArea=Microsoft.Subscriptions/subscriptions;
description=Consider virtual machine reserved instance to save over your on-demand costs;
recommendationText=Consider virtual machine reserved instance to save over your on-demand costs;
recommendationTypeId=xxx; instanceName=xxxx;
additionalInfo=;
tags=;
ResourceId=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxxx}

字符串
在这里,我想访问一个字段“savingsCurrency”以在if语句中进行比较。该字段嵌套在“additionalInfo”中,而“additionalInfo”又在“AdditionalRecommendation”对象中。
我现在有以下代码:

Write-Output "advisorrecommendation: $advisorRecommendation"

if (-not([string]::IsNullOrEmpty($advisorRecommendation.additionalInfo)))
{
    $additionalInfo = $advisorRecommendation.additionalInfo | ConvertTo-Json
    Write-Output "additionalInfo: $additionalInfo"
    if (-not([string]::IsNullOrEmpty($additionalInfo.savingsCurrency)))
    {
        $savingsCurrency = $additionalInfo.savingsCurrency | ConvertTo-Json
        Write-Output "Currency: $savingsCurrency"
    }
}
else
{
    $additionalInfo = $null
}


这段代码给出了以下输出:

advisorrecommendation:
@{id=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxx; category=Cost; impact=High; impactedArea=Microsoft.Subscriptions/subscriptions; description=Consider virtual machine reserved instance to save over your on-demand costs; recommendationText=Consider virtual machine reserved instance to save over your on-demand costs; recommendationTypeId=xxxx; instanceName=xxxx; additionalInfo=; tags=; ResourceId=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxxx}
additionalInfo: {
    "annualSavingsAmount": "697",
    "reservedResourceType": "virtualmachines",
    "targetResourceCount": "4",
    "savingsCurrency": "USD",
    "savingsAmount": "58",
    "lookbackPeriod": "30",
    "displaySKU": "Standard_B2s",
    "displayQty": "4",
    "location": "westeurope",
    "region": "westeurope",
    "vmSize": "Standard_B2s",
    "subId": "xxxx",
    "scope": "Single",
    "term": "P1Y",
    "qty": "4",
    "sku": "Standard_B2s"
}

的字符串
“savingsCurrency”字段永远不会通过null/empty检查,即使我可以清楚地看到“additionalInfo”中的字段不是空的或null。这里出了什么问题?如果我在没有前面的存在检查的情况下尝试打印,它只是打印additionalInfo对象,并像这样追加.savingsCurrency:

savingsCurrency: {
    "annualSavingsAmount": "697",
    "reservedResourceType": "virtualmachines",
    "targetResourceCount": "4",
    "savingsCurrency": "USD",
    "savingsAmount": "58",
    "lookbackPeriod": "30",
    "displaySKU": "Standard_B2s",
    "displayQty": "4",
    "location": "westeurope",
    "region": "westeurope",
    "vmSize": "Standard_B2s",
    "subId": "xxxx",
    "scope": "Single",
    "term": "P1Y",
    "qty": "4",
    "sku": "Standard_B2s"
}.savingsCurrency

c0vxltue

c0vxltue1#

$advisorRecommendation.additionalInfo是一个带有嵌套属性的 object

$advisorRecommendation.additionalInfo.savingsCurrency
# USD

字符串
但是$additionalInfo是一个 string,因为你在赋值之前已经将对象转换为json:

$additionalInfo = $advisorRecommendation.additionalInfo | ConvertTo-Json

$additionalInfo.GetType().FullName
# System.String


因此,$savingsCurrency = $additionalInfo.savingsCurrency试图读取 stringsavingsCurrency属性,而字符串没有该属性,因此您得到$null
你想做的是保留对$advisorRecommendation.additionalInfoobject 的引用,并只将其转换为json,以便将其写入控制台:

Write-Output "advisorrecommendation: $advisorRecommendation"

#   vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
if ($advisorRecommendation.additionalInfo -ne $null)
#   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{

    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    $additionalInfo = $advisorRecommendation.additionalInfo
    Write-Output "additionalInfo: $($additionalInfo | ConvertTo-Json -Depth 99)"
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    if (-not([string]::IsNullOrEmpty($additionalInfo.savingsCurrency)))
    {
        $savingsCurrency = $additionalInfo.savingsCurrency
        Write-Output "Currency: $savingsCurrency"
    }
}
else
{
    $additionalInfo = $null
}


您现在应该在输出中看到Currency: USD
注意,$additionalInfo.savingsCurrency已经是一个 string,所以你不需要使用ConvertTo-Json来显示这个值,除非你特别希望它被格式化为一个json字符串(即"USD"带有引号和任何特殊字符,如\转义,而不是字面字符串值USD

9o685dep

9o685dep2#

“savingsCurrency”字段从未通过null/empty检查,即使我可以清楚地看到“additionalInfo”中的字段不是空的或null。这里出了什么问题?
您可以检查not null和if条件如下:

$additionalInfo = $additionalInfo | ConvertFrom-Json
if (-not([string]::IsNullOrEmpty($additionalInfo.savingsCurrency)))
    {
        $savingsCurrency = $additionalInfo.savingsCurrency | ConvertTo-Json
        Write-Output "Currency: $savingsCurrency"
    }

字符串
x1c 0d1x的数据
如何在PowerShell中访问JSON字段
要访问任何json文件,您需要首先将其从json转换为如下所示:

$additionalInfo = $additionalInfo | ConvertFrom-Json
$savingsCurrency= $savingsCurrency | ConvertFrom-Json


要访问任何字段,您可以使用点运算符:

$additionalInfo.savingsCurrency


相关问题