首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用powershell从json文件中删除对象

使用powershell从json文件中删除对象
EN

Stack Overflow用户
提问于 2020-05-14 11:49:19
回答 1查看 638关注 0票数 0

在这个问题中,我想要完成的是,我正在尝试删除json文件中的特定对象。但在这样做的同时,我遇到了一些困难,我试图参考文章Iterate over JSON and remove JSON element in PowerShell

和实现相同,但是它删除了整个元素,但是我想删除元素中的一个特定对象,而不是整个元素,下面是必需的内容

代码语言:javascript
复制
1. json file

    {
        "name":  "JourneyPack",
        "description":  "Details of the journey across india",
        "author":  "Sachin",
        "version":  "1.0.0",
        "main":  "main.js",
        "build":  {
                      "applicationID":  "desktop",
                      "Necessaryfiles":  [
                                    "main.js",
                                    "package.json",

                                ],
                      "Storage":  {
                                          "output":  "./reunited"
                                      },
                      "DeatilsOfJourney":  [
                                             {
                                                 "from":  "../Pune",
                                                 "to":  "../travel/Pune",
                                                 "filter":  [
                                                                "**/*
                                                            ]
                                             },
                                             {
                                                 "from":  "../Delhi",
                                                 "to":  "../travel/Delhi",
                                                 "filter":  [
                                                                "**/*"
                                                            ]
                                             },
                                             {
                                                 "from":  "../Jharkhand",
                                                 "to": "../travel/Jharkhand",
                                                 "filter":  [
                                                                "**/*"
                                                            ]
                                             },
                                         ],
                      "IOS":  {
                                  "category":  "desktop"
                              },
                      "Windows":  {
                                  "icon":  "images/desktopicons/icons.ico",
                                  "target":  [
                                                 "divfrieght"
                                             ],
                                  "publisherName":  [
                                                        "Sachin"
                                                    ]
                              },
                      "divfrieght":  {
                                   "PointClick":  true,
                                   "standaloneMachine":  true,
                                   "allowrise":  true,
                                   "allowinstdir":  true,
                                   "menu":  "JourneyPack"

                               }
                  },
        "private":  true,

    }

下面是再次尝试过的代码这是我从Iterate over JSON and remove JSON element in PowerShell 2引用的代码。

代码语言:javascript
复制
    $inputFile  = '<THE FULL PATH AND FILENAME TO YOUR JSON FILE>'
    $outputFile = '<THE FULL PATH AND FILENAME FOR THE OUTPUT JSON FILE>'

    $apijson = Get-Content -Path $inputFile -Raw | ConvertFrom-Json

    # for safety, first make a copy of the original .paths object
    $newPaths = $apijson.paths


    foreach ($element in $newPaths.PSObject.Properties) {
        $objName = $element.Name
        $objValue = $element.Value
        $objProperties = $objValue.PSObject.Properties
        foreach ($prop in $objProperties) {
            if ($prop.Value.'from' -eq 'Jharkhand') {
                $propName = $prop.Name
                $objProperties.Remove($propName)
                Write-Host "Removed object $objName -- $propName"
            }
        }
    }

    # now overwrite the $apijson.paths with this cleaned up version
    $apijson.paths = $newPaths

    # I assume you want to convert it back to a .JSON file??
    $apijson | ConvertTo-Json -Depth 100 | Set-Content -Path $outputFile -Force

我想删除"from“等于"../Jharkhand/”的对象。

期望输出

代码语言:javascript
复制
{
    "name":  "JourneyPack",
    "description":  "Details of the journey across india",
    "author":  "Sachin",
    "version":  "1.0.0",
    "main":  "main.js",
    "build":  {
                  "applicationID":  "desktop",
                  "Necessaryfiles":  [
                                "main.js",
                                "package.json",

                            ],
                  "Storage":  {
                                      "output":  "./reunited"
                                  },
                  "DeatilsOfJourney":  [
                                         {
                                             "from":  "../Pune",
                                             "to":  "../travel/Pune",
                                             "filter":  [
                                                            "**/*
                                                        ]
                                         },
                                         {
                                             "from":  "../Delhi",
                                             "to":  "../travel/Delhi",
                                             "filter":  [
                                                            "**/*"
                                                        ]
                                         },
                                     ],
                  "IOS":  {
                              "category":  "desktop"
                          },
                  "Windows":  {
                              "icon":  "images/desktopicons/icons.ico",
                              "target":  [
                                             "divfrieght"
                                         ],
                              "publisherName":  [
                                                    "Sachin"
                                                ]
                          },
                  "divfrieght":  {
                               "PointClick":  true,
                               "standaloneMachine":  true,
                               "allowrise":  true,
                               "allowinstdir":  true,
                               "menu":  "JourneyPack"

                           }
              },
    "private":  true,

}

如果有人能帮上忙,那将是非常有帮助的

EN

回答 1

Stack Overflow用户

发布于 2020-05-14 14:44:37

".paths“属性不属于您的json文件,因此删除了脚本的这一部分。

代码语言:javascript
复制
# for safety, first make a copy of the original .paths object
$newPaths = $apijson.paths

尝试以下代码:

代码语言:javascript
复制
$inputFile  = 'input.json'
$outputFile = 'output.json'

$apijson = Get-Content -Path $inputFile -Raw | ConvertFrom-Json

foreach ($element in $apijson.PSObject.Properties) {
    $objName = $element.Name
    $objValue = $element.Value
    $objProperties = $objValue.PSObject.Properties
    foreach ($prop in $objProperties) {
        # Your object lies in this array
        if ($prop.Name -eq 'DeatilsOfJourney') {   
            [System.Collections.ArrayList]$arr = $prop.Value
            #Iterate over your array and find that object which you want to remove
            for ($i = 0; $i -lt $arr.count; $i++) {
                if ($arr[$i].'from' -eq '../Jharkhand')
                {
                    $arr.RemoveAt($i)
                    $i--
                }
            }
            $prop.Value = $arr
        }
    }
}

$apijson | ConvertTo-Json -Depth 100 | Set-Content -Path $outputFile -Force
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61788995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档