How do I remove elements from in-memory JSON?

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 5 months, 2 weeks ago.

  • Author
    Posts
  • #44040
    Profile photo of GS
    GS
    Participant

    Hello,

    I have JSON which has collection of elements and I need to remove and element and then save JSON. I'm lost as far as what do I need to use to remove an element.

    PS C:\windows\system32> $jsonFile.resourcePackages
    
    resourcePackage                                                         
    ---------------                                                         
    @{name=SynopticPanelBySQLBI1447436165337; type=0; items=System.Object[]}
    @{name=YourVisual1465156444568; type=0; items=System.Object[]}          
    @{name=BulletChartBySQLBI1459586236884; type=0; items=System.Object[]}  
    @{name=CandleStickBySQLBI1458002061454; type=0; items=System.Object[]}  
    @{name=SmartFilterBySQLBI1465232218990; type=0; items=System.Object[]}  
    @{name=SmartFilterBySQLBI1465231403311; type=0; items=System.Object[]}  
    
    
    
    PS C:\windows\system32> $jsonFile.resourcePackages.Remove(0)
    Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."
    At line:1 char:1
    + $jsonFile.resourcePackages.Remove(0)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotSupportedException
  • #44705
    Profile photo of Don Jones
    Don Jones
    Keymaster

    If you've turned this into a proper object (ConvertFrom-Json), you could...

    $json | where { $_.whatever -eq 'something' } | convertto-json | out-file whatever.js

    As a kind of basic approach. Use Where-Object to filter out the item(s) you don't want.

You must be logged in to reply to this topic.