gpt4 book ai didi

json - 在 Powershell 中转换为 JSON 时如何排除非值对象属性

转载 作者:行者123 更新时间:2023-12-04 03:20:02 26 4
gpt4 key购买 nike

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

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

  • 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 ""

    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"
    }

    关于json - 在 Powershell 中转换为 JSON 时如何排除非值对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038848/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com