从较大的PowerShell对象创建新的JSON对象 [英] Creating new JSON objects from larger PowerShell objects

查看:367
本文介绍了从较大的PowerShell对象创建新的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个我通过Invoke-RestMethod检索到的大型PowerShell对象中,我希望将其缩小以选择属性并创建一个新的(较小的)对象,然后将其转换为JSON数组.

From a large PowerShell Object that I have retrieved via an Invoke-RestMethod, I am looking to shrink this down to select attributes and create a new (smaller) object that I can then convert into a JSON array.

样品集由'脚本专家':

$request = 'http://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
$output = Invoke-WebRequest $request | ConvertFrom-Json
$output

给我:

type-id        : e431f5f6-b5d2-343d-8b36-72607fffb74b
name           : Nirvana
ipis           : {}
disambiguation : 90s US grunge band
country        : US
life-span      : @{end=1994-04-05; ended=True; begin=1988-01}
sort-name      : Nirvana
isnis          : {0000000123486830}
aliases        : {@{name=Nirvana US; type-id=; sort-name=Nirvana US; end=; 
begin=; primary=; type=; locale=; ended=False}}
begin_area     : @{name=Aberdeen; disambiguation=; sort-name=Aberdeen; 
id=a640b45c-c173-49b1-8030-973603e895b5}
area           : @{sort-name=United States; id=489ce91b-6658-3307-9877-795b68554c98; iso-3166-1-codes=System.Object[]; disambiguation=; name=United States}
type           : Group
id             : 5b11f4ce-a62d-471e-81fc-a69a8278c7da
end_area       : 
gender         : 
gender-id      : 

如果我在根级别属性上使用select选项,则可以按预期工作:

If I use the select option on the root-level attributes, this works as expected:

$request = 'http://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-
a69a8278c7da?inc=aliases&fmt=json'
$output = Invoke-WebRequest $request | ConvertFrom-Json
$output | select name, disambiguation | ConvertTo-Json

输出:

{
    "name":  "Nirvana",
    "disambiguation":  "90s US grunge band"
}

但是,如果我尝试添加嵌套属性之一,那么它就无法按照我希望的那样工作.

But if I try to add one of the nested attributes, it doesn't work as I'd hope...

$output | select name, disambiguation, area.sortname | ConvertTo-Json
{
    "name":  "Nirvana",
    "disambiguation":  "90s US grunge band",
    "area.sort-name":  null # <-- Expect "United States"
}

我想看的东西:

{
    "name":  "Nirvana",
    "disambiguation":  "90s US grunge band",
    "area":  {
        "sort-name":  "United States"
    }
}

我也尝试扩展数组,但这似乎丢失了我要保留的根级别字段:

I've also tried expanding the array, but this seems to lose my root level fields which I want to retain:

$output | select -expand area | select name, disambiguation, sort-name | ConvertTo-Json
{
    "name":  "United States", # <-- Taken from the area.name value
    "disambiguation":  "",    # <-- Lost when expanding
    "sort-name":  "United States"
}

任何建议/指针都非常感谢!

Any suggestions/pointers greatly appreciated!

推荐答案

由TessellatingHeckler提供

As provided by TessellatingHeckler

select name, @{Name='sort-name'; Expression={$_.area.{sort-name}}}

这篇关于从较大的PowerShell对象创建新的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆