在Powershell中转换为JSON时如何排除非值对象属性 [英] How to exclude non-valued object properties when converting to JSON in Powershell

查看:63
本文介绍了在Powershell中转换为JSON时如何排除非值对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段有效的代码,但是我想知道是否有更好的方法来做到这一点.到目前为止,我找不到任何相关的信息.这是事实:

I have a piece of code that works but I want to know if there is a better way to do it. I could not find anything related so far. Here are the facts:

  • 我有一个具有n个属性的对象.
  • 我想使用(ConvertTo-Json)将此对象转换为JSON.
  • 我不想在JSON中包含那些未赋值的对象属性.

构建对象(不是很重要)

Building the object (not really important):

$object = New-Object PSObject
Add-Member -InputObject $object -MemberType NoteProperty -Name TableName -Value "MyTable"
Add-Member -InputObject $object -MemberType NoteProperty -Name Description -Value "Lorem ipsum dolor.."
Add-Member -InputObject $object -MemberType NoteProperty -Name AppArea -Value "UserMgmt"
Add-Member -InputObject $object -MemberType NoteProperty -Name InitialVersionCode -Value ""

我需要改进的行(以滤除非值属性,并且不将其包含在JSON中)

The line that I need improvements (to filter out the non-valued properties and not include them in the JSON)

# So I want to 'keep' and deliver to the JSON only the properties that are valued (first 3).
$object | select -Property TableName, Description, AppArea, InitialVersion | ConvertTo-Json

此行提供的内容:

What this line delivers:

Results:
{
    "TableName":  "MyTable",
    "Description":  "Lorem ipsum dolor..",
    "AppArea":  "UserMgmt",
    "InitialVersion":  null
}

What I want to obtain:
{
    "TableName":  "MyTable",
    "Description":  "Lorem ipsum dolor..",
    "AppArea":  "UserMgmt"
}

我尝试过并且可以工作的东西,但是我不喜欢它,因为我有更多的属性要处理:

What I've tried and works, but I don't like it since I have much more properties to handle:

$JSON = New-Object PSObject

if ($object.TableName){
   Add-Member -InputObject $JSON -MemberType NoteProperty -Name TableName -Value $object.TableName
}

if ($object.Description){
   Add-Member -InputObject $JSON -MemberType NoteProperty -Name Description -Value $object.Description
}

if ($object.AppArea){
   Add-Member -InputObject $JSON -MemberType NoteProperty -Name AppArea -Value $object.AppArea
}

if ($object.InitialVersionCode){
   Add-Member -InputObject $JSON -MemberType NoteProperty -Name InitialVersionCode -Value $object.InitialVersionCode
}

$JSON | ConvertTo-Json

推荐答案

像这样吗?

$object = New-Object PSObject

Add-Member -InputObject $object -MemberType NoteProperty -Name TableName -Value "MyTable"
Add-Member -InputObject $object -MemberType NoteProperty -Name Description -Value "Lorem ipsum dolor.."
Add-Member -InputObject $object -MemberType NoteProperty -Name AppArea -Value "UserMgmt"
Add-Member -InputObject $object -MemberType NoteProperty -Name InitialVersionCode -Value ""

# Iterate over objects
$object | ForEach-Object {
    # Get array of names of object properties that can be cast to boolean TRUE
    # PSObject.Properties - https://msdn.microsoft.com/en-us/library/system.management.automation.psobject.properties.aspx
    $NonEmptyProperties = $_.psobject.Properties | Where-Object {$_.Value} | Select-Object -ExpandProperty Name

    # Convert object to JSON with only non-empty properties
    $_ | Select-Object -Property $NonEmptyProperties | ConvertTo-Json
}

结果:

{
    "TableName":  "MyTable",
    "Description":  "Lorem ipsum dolor..",
    "AppArea":  "UserMgmt"
}

这篇关于在Powershell中转换为JSON时如何排除非值对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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