1

I have a test.json file which has the below structure:

{
  "name": "test",
  "class": "4",
  "exam": "test",
  "marks": "4"
}

I want to remove some pairs from it like exam and class, and ultimately it should look like below:

{
  "name": "test",
  "marks": "4"
}

How can I do it from PowerShell?

2 Answers 2

3

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

Comments

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"
}

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.