I have a JSON which contains 3 objects in a key => value manner. This JSON is wrapped into an Array. So now, I have an array which consists of JSON objects.
What i'm trying to do here is to remove one of the JSON's object (not the complete JSON element) from the array.
Any ideas to do the needful?
Sample JSON:
{
'd_id' => '1',
'name' => 'abc',
'mapping' => 'xyz'
}
Sample Array
A = [{
'd_id' => '1',
'name' => 'abc',
'mapping' => 'xyz'
},
{
'd_id' => '2',
'name' => 'abc',
'mapping' => 'xyz'
},
{
'd_id' => '3',
'name' => 'abc',
'mapping' => 'xyz'
}]
Here is what I have tried:
- Used an ArrayList instead of an Array to get the desired output.
- Used 'Remove' to clean up element in ArrayList.
powershell code:
$j_res = new-object collections.generic.list[object]
For ($i = 0; $i -lt 5; $i++){
$ret = ConvertFrom-Json "{}"
$ret | Add-Member -Name 'D_Id' -Value $i -MemberType NoteProperty
$ret | Add-Member -Name 'Name' -Value 'axz' -MemberType NoteProperty
$ret | Add-Member -Name 'Mapping' -Value 'byz' -MemberType NoteProperty
$j_res.Add($ret)
}
$j_res.Remove($j_res[0].Mapping)
Write-Host 'The is required output is' $j_res
Expected:
A = [{
'd_id' => '1',
'name' => 'abc'
},
{
'd_id' => '2',
'name' => 'abc'
},
{
'd_id' => '3',
'name' => 'abc'
}]
Actual:
A = [{
'd_id' => '1',
'name' => 'abc',
'mapping' => 'xyz'
},
{
'd_id' => '2',
'name' => 'abc',
'mapping' => 'xyz'
},
{
'd_id' => '3',
'name' => 'abc',
'mapping' => 'xyz'
}]