# helper to turn PSCustomObject into a list of key/value pairs
function Get-ObjectMembers {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True)]
[PSCustomObject]$obj
)
$obj | Get-Member -MemberType NoteProperty | ForEach-Object {
$key = $_.Name
[PSCustomObject]@{Key = $key; Value = $obj."$key"}
}
}
$entry = [PSCustomObject]@{
PostedDate = "04/18/2018"
JobTitle = "King"
Street = "Bloor"
City = "Toronto"
DocumentURL = "../path/to/file.pdf"
}
$path = "A:\path\to\file.json"
$entry = Get-Content $path | ConvertFrom-Json
$entry
$today = (Get-Date).ToString('MM/dd/yyyy')#Get-Date -Date -format MM/dd/yyyy
$today = [datetime]::ParseExact($today, "MM/dd/yyyy", [System.Globalization.CultureInfo]::CurrentCulture)
foreach($date in $entry.Closing)
{
$newdate = Get-Date $date.ToString()
$newdate = $newdate.ToString('MM/dd/yyyy')
$newdate = [datetime]::ParseExact($newdate, "MM/dd/yyyy", [System.Globalization.CultureInfo]::CurrentCulture)
if($today -gt $newdate)
{
Write-Host $date
#remove element from the JSON list
}
}
我不知道如何从 JSON 对象中删除元素并将已删除项目的副本保存为不同的 JSON 文件
我在 Windows 10 上使用 PowerShell 5.1
“我不知道如何从 JSON 对象中删除元素
并将已删除项目的副本保存为不同的 JSON 文件”
你总是可以利用地点对象
$entry | where { $_.whatever -ne 'something' } | convertto-json | out-file whatever.js
要根据属性删除节点,请尝试以下操作:
$entry.Section.Whatever = $entry.Section.Whatever| Select-Object * -ExcludeProperty Something
要覆盖现有的 JSON 对象,请尝试如下操作:
$entry = $entry | where { $_.whatever -ne 'something' }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)