如何删除嵌套的JSON对象成员

发布于 2025-02-04 17:56:55 字数 1517 浏览 2 评论 0原文

我正在尝试从以下JSON文件中删除length值对:

{
    "uuid":  "6f74b1ba-0d7c-4c85-955b-2a4309f0e8df",
    "records":  {
                    "record1":  [
                                    {
                                        "locale":  "en_US",
                                        "category":  "alpha",
                                        "contents":  "My hovercraft is full of eels",
                                        "length":  29
                                    }
                                ],
                    "record2":  [
                                    {
                                        "locale":  "cs_CZ",
                                        "category":  "alpha",
                                        "contents":  "Moje vznášedlo je plné úhořů",
                                        "length":  28
                                    }
                                ]
                }
}

即使显然找到了length属性,也未删除,因为输出文件与输入文件。

我正在使用以下代码:

$infile = "C:\Temp\input.json"
$outfile = "C:\Temp\output.json"

$json = Get-Content $infile -Encoding UTF8 | ConvertFrom-Json
$records = $json.records

$records.PSObject.Properties | ForEach-Object {
    if (($_.Value | Get-Member -Name "length")) {
        Write-Host "length property found."
        $_.Value.PSObject.Properties.Remove("length")
    }
}

$json | ConvertTo-Json -Depth 3 | Out-File $outfile -Encoding UTF8

我在做什么错?

I'm trying to remove the length value pair from the following JSON file:

{
    "uuid":  "6f74b1ba-0d7c-4c85-955b-2a4309f0e8df",
    "records":  {
                    "record1":  [
                                    {
                                        "locale":  "en_US",
                                        "category":  "alpha",
                                        "contents":  "My hovercraft is full of eels",
                                        "length":  29
                                    }
                                ],
                    "record2":  [
                                    {
                                        "locale":  "cs_CZ",
                                        "category":  "alpha",
                                        "contents":  "Moje vznášedlo je plné úhořů",
                                        "length":  28
                                    }
                                ]
                }
}

Even though the length property is apparently found, it's not deleted, because the output file is identical to the input file.

I'm using the following code:

$infile = "C:\Temp\input.json"
$outfile = "C:\Temp\output.json"

$json = Get-Content $infile -Encoding UTF8 | ConvertFrom-Json
$records = $json.records

$records.PSObject.Properties | ForEach-Object {
    if (($_.Value | Get-Member -Name "length")) {
        Write-Host "length property found."
        $_.Value.PSObject.Properties.Remove("length")
    }
}

$json | ConvertTo-Json -Depth 3 | Out-File $outfile -Encoding UTF8

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2025-02-11 17:56:55

记录*属性是数组,因此您需要一个嵌套循环来处理它们:

foreach( $property in $records.PSObject.Properties ) {
    foreach( $recordItem in $property.Value ) {
       if( $recordItem | Get-Member -Name 'length' ) {
           $recordItem.PSObject.Properties.Remove( 'length' )
       }
   } 
}

为了清晰度和性能,我已通过> foreach-object命令更换了foreach语句。尤其是在嵌套循环中,foreach有助于提高清晰度,因为我们不再需要考虑自动$ _变量的上下文。另外,foreach语句速度更快,因为它不涉及管道开销。

The record* properties are arrays, so you need a nested loop to process them:

foreach( $property in $records.PSObject.Properties ) {
    foreach( $recordItem in $property.Value ) {
       if( $recordItem | Get-Member -Name 'length' ) {
           $recordItem.PSObject.Properties.Remove( 'length' )
       }
   } 
}

For code clarity and performance I've replaced the ForEach-Object command by the foreach statement. Especially in nested loops, foreach helps to improve clarity as we no longer have to think about the context of the automatic $_ variable. Also the foreach statement is faster as it doesn't involve pipeline overhead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文