How do I remove all elements in the below JSON where 'Name = "Test HD Module 2" and Team = "Help Desk"?'
{
"Content": {
"Modules": [
{
"ID": 2,
"Name": "Test HD Module 2",
"Description": "This module opens the Google website.",
"Team": "Help Desk",
"Is_Admin": "No",
"Location": "Server",
"Client_Facing": "No",
"Action": "Website",
"Image_Name": "teamviewer",
"Image_Path": "C:Temp/Userss",
"Is_Popup": false,
"Popup_Text": "",
"Submodule": "false",
"Is_Editable": "true"
},
{
"ID": 3,
"Name": "Test HD Module 2",
"Team": "Server",
"Description": "This module opens the website.",
"Is_Admin": "No",
"Location": "Server",
"Client_Facing": "No",
"Action": "Website",
"Image_Name": "device-info",
"Image_Path": "C:Temp/Userss",
"Is_Popup": true,
"Popup_Text": "Are you sure you want to run Test HD Module 3?",
"Submodule": "false",
"Is_Editable": "false"
}
],
"Sub-Modules": [
{
"ID": 4,
"Name": "Test HD Sub-Module 3",
"Team": "Help Desk",
"Is_Admin": "No",
"Location": "Server",
"Client_Facing": "No",
"Action": "CMD",
"Action_Text": "",
"Image_Name": "device-info",
"Image_Path": "C:Temp/Users",
"Is_Popup": "false",
"Popup_Text": "",
"Submodule": "true",
"Is_Editable": "false"
}
]
}
}
I've tried using below but have had no luck.
$JSON | Where-Object {$_.Modules.Name -ne "Test HD Module 2" -and $_.Modules.Team -ne "Help Desk" }
Is there an easier way to filter a JSON? I basically want the same JSON but with those elements removed (I need to keep the 'Sub-Modules' information). Is the Where-Object the correct route when filtering a JSON?
CodePudding user response:
I believe the expression you're looking for is:
-not ($_.Name -eq "Test HD Module 2" -and $_.Team -eq "Help Desk")
Different from the expression you currently have, which would for example, exclude any object where Name != Test HD Module 2
.
If you want to update your object you can use the following:
$json = Get-Content path\to\myjson.json -Raw | ConvertFrom-Json
$json.Content.Modules = @(
$json.Content.Modules | Where-Object {
-not ($_.Name -eq "Test HD Module 2" -and $_.Team -eq "Help Desk")
}
)
$json | ConvertTo-Json -Depth 99 | Set-Content path\to\mynewjson.json
The array subexpression operator @(...)
ensures that the Modules
property will become an array.
Resulting Json using the example in question would become:
{
"Content": {
"Modules": [
{
"ID": 3,
"Name": "Test HD Module 2",
"Team": "Server",
"Description": "This module opens the website.",
"Is_Admin": "No",
"Location": "Server",
"Client_Facing": "No",
"Action": "Website",
"Image_Name": "device-info",
"Image_Path": "C:Temp/Userss",
"Is_Popup": true,
"Popup_Text": "Are you sure you want to run Test HD Module 3?",
"Submodule": "false",
"Is_Editable": "false"
}
],
"Sub-Modules": [
{
"ID": 4,
"Name": "Test HD Sub-Module 3",
"Team": "Help Desk",
"Is_Admin": "No",
"Location": "Server",
"Client_Facing": "No",
"Action": "CMD",
"Action_Text": "",
"Image_Name": "device-info",
"Image_Path": "C:Temp/Users",
"Is_Popup": "false",
"Popup_Text": "",
"Submodule": "true",
"Is_Editable": "false"
}
]
}
}