'Removing Objects from JSON using powershell
i have a test.json file which has below structure:
{
"name": "test",
"class": "4",
"exam": "test",
"marks": "4"
}
i want to remove some pairs from it like exam and class, nd ultimately it should look like below:
{
"name": "test",
"marks": "4"
}
how can i do it from powershell?
Solution 1:[1]
Your post was not completely clear if you wanted to remove certain keys, or if you only wanted to retain marks and name. The code below performs the latter:
Get-Content 'test.json' -Raw |
ConvertFrom-Json |
Select-Object name, marks |
ConvertTo-Json
Result:
{
"name": "test",
"marks": "4"
}
Solution 2:[2]
powershell cmd:
$obj = Get-Content .\aaa.json | ConvertFrom-Json
$obj.psobject.properties.remove('exam')
$obj.psobject.properties.remove('class')
$obj | ConvertTo-Json
output:
{
"name": "test",
"marks": "4"
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DocZerø |
| Solution 2 | Sage Pourpre |
