json - 如何从我的 JSON 对象中删除条目?
问题描述
# 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 文件”
你总是可以使用Where-Object
$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' }
推荐阅读
- python - 在已经有 2.7 的 linux 服务器中安装 python 3.9,没有任何副作用
- python - 如何使用 django 将图像作为 BLOB 直接保存到 MySQL 数据库中?这些图片是通过 django 管理面板动态上传的?
- flutter - 白屏问题flutter web
- android - 托管 Google Play 中的 AppUpdateManager?
- reactjs - React Material UI 组件。函数还是类?
- c++ - 如何缩短此代码以避免冗余?
- sql - 查询返回日期行之间的日期范围
- css - 输入字段对齐
- c - 是否可以使用 for 循环编写“else if”N 次?
- r - 如何使用 substr() 删除 mpg$trans 数据中的 NA