-1

In this Question i want to accomplish is that i am trying to delete a specific object in the json file. But while doing so i am experiencing some difficulties i tried to refer article Iterate over JSON and remove JSON element in PowerShell

and implement the same but however it is deleting the entire element but i want to delete a specific object in the element not the entire element following are the required things

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,

    }

following is the tried code again this is that i have referred from Iterate over JSON and remove JSON element in PowerShell 2. tried code

    $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

i want to delete the object where "from" is equal to "../Jharkhand/"

Desired Output

{
    "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,

}

if anyone could help that would be really helpful

2
  • 1
    The JSON file is broken. Please edit the question and add a working sample. Commented May 14, 2020 at 6:12
  • Did you tried the below code? Commented May 18, 2020 at 5:15

1 Answer 1

0

".paths" property does not belong to you json file, so removed this part of your script.

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

Try this code:

$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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.