json - 从类型 JSON 属性中提取属性

标签 json powershell

我的结构中有一些从 Azure 管理 cmdlet Get-UsageAggregates 返回的 Azure 使用情况数据。它在 Json 类型的属性中包含一些信息,我想从中提取子属性。

这些数据包含在变量$usage中。如果我执行

$usage.UsageAggregations.properties | 
    select-object metercategory,metersubcategory,unit,quantity
                 ,@{Name="resource"; Expression={$_.InstanceData }}

我得到了这样的对象列表:

MeterCategory    : Storage
MeterSubCategory : Geo Redundant
Unit             : GB
Quantity         : 3.76344
resource         : {"Microsoft.Resources":{"resourceUri":"/subscriptions/[GUID]/resourceGroups/default-storage-
                   northeurope/providers/Microsoft.ClassicStorage/storageAccounts/MyAccountName","location":"eunorth"}}

我想看到“MyAccountName”而不是 JSON 字符串。我怎样才能做到这一点?

我尝试使用

$usage.UsageAggregations.properties | 
    select-object metercategory,metersubcategory,unit,quantity
                 ,@{Name="resource"; 
                    Expression={$_.InstanceData | 
                                ConvertFrom-Json | 
                                select-object -Property Microsoft.Resources.resourceuri }}

但是输出是

MeterCategory    : Storage
MeterSubCategory : Geo Redundant
Unit             : GB
Quantity         : 3.76344
resource         :  @{Microsoft.Resources.resourceuri=}

resourceuri 之后添加 .value 也没有帮助。

最佳答案

您问题的要点是如何从以下 JSON 字符串(打印精美且简化)中提取字符串 MyAccountName:

$json = @'
{
  "Microsoft.Resources": {
    "resourceUri":  "/subscriptions/[GUID]/resourceGroups/.../MyAccountName",
    "location":  "eunorth"
  }
}
'@

ConvertFrom-Json-split 运算符组合:

(($json | ConvertFrom-Json).'Microsoft.Resources'.resourceUri -split '/')[-1]
  • ConvertFrom-Json 将 JSON 字符串转换为具有顶级属性 Microsoft.Resources 属性的自定义对象,而该属性又是一个具有以下属性的自定义对象:属性resourceUrilocation

  • .'Microsoft.Resources'.resourceUri 因此返回 resourceUri JSON 属性的值 - 请注意需要引用 'Microsoft.Resources',以确保其嵌入 . 被视为属性名称的一部分,而不是作为属性名称之间的分隔符。

  • -split '/' 将值按 / 拆分为标记,并将它们作为数组返回。

  • [-1] 引用该数组的最后元素,在本例中为 MyAccountName


至于你尝试过的:

Select-Object 不支持属性路径,仅支持顶级属性名称
换句话说,您只能从输入对象中提取顶级属性;您无法深入研究它们。

因此,您可以提取顶级属性 Microsoft.Resources - 其名称恰好包含 . 本身 - 但也不能提取 resourceUri 属性。

如果您尝试这样做,整个参数将被视为顶级属性名称,字面名称为 Microsoft.Resources.resourceUri
如果输入对象没有此类属性,则会为输出对象创建该属性,其值为 $null[1 ]


通常,使Select-Object提取属性 - 与返回自定义对象的默认行为相反对于该属性,您必须使用-ExpandProperty而不是-Property

请注意,如果您使用了-ExpandProperty,问题会变得更加明显,因为 PowerShell 会报告错误如果找不到给定名称的(顶级)属性[1]:

> $json | ConvertFrom-Json | Select-Object -ExpandProperty Microsoft.Resources.resourceUri
Select-Object : Property "Microsoft.Resources.resourceUri" cannot be found.
...

[1]但奇怪的是,如果您尝试引用 . 分隔字符串的组件Select-Object 只需通过 - 传递输入对象 请参阅this GitHub issue .

关于json - 从类型 JSON 属性中提取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663107/

相关文章:

powershell - GitHub 操作 : Pass Environment Variable to into Action using PowerShell

xml - Powershell无法读取xml文件?

powershell - 使用 Powershell 获取 Azure WebApp 的 PublishProfile

powershell - 在 PowerShell 控制台中对 native 命令行应用程序的输出日志进行着色

JavaScript 异常 : is not a function

c# - 如何使用 System.Text.Json 将 Newtonsoft JToken 序列化为 JSON?

c# - 从c#中的json字符串中查找所有出现的值

powershell - InstallShield Automation 无法创建对象

javascript - 如何从以下 json 中获取 key

android - 如何使用 Retrofit 和 Gson 获取外键字段?