1

I am trying to get the list of "enabled" features from featureset array from below JSON snippet with powershell. output expected :

feature1, feature2, feature4

I tried this piece of code but I was only able to get to the specific index of the array, not all elements, also not able to put condition for "enable" = true :

$output = foreach( $feature in $jsn.abc.comp1.featureset[0].psobject.Properties ) {
   [PSCustomObject]@{
       featureName = $feature.Name
       featureValue = $feature.Value
   }
}

json:

{
"abc": {
    "comp1": {
        "id": "1308",
        "featureset":[
            {
                "name": "feature1",
                "enable" : true,
                "ID": "0670FF495355878281174937"
            },
            {
                "name": "feature2",
                "enable" : true,
                "ID": "0670FF495355878281174937"
            },
            {
                "name": "feature3",
                "enable" : false,
                "ID": "0670FF495355878281174937"
            },
            {
                "name": "feature4",
                "enable" : true,
                "ID": "0670FF495355878281174937"
            }
        ]
      }
    }
  }
3
  • I think you're looking for $json.abc.comp1.featureset | ? enable Commented Feb 9, 2023 at 14:30
  • Thanks, this filters enabled features but returns complete array, how only name key and its value can be retrieved for enabled features ? Commented Feb 9, 2023 at 14:47
  • using a loop like Mathias did in his answer. $json.abc.comp1.featureset | ? enable | % name would also work Commented Feb 9, 2023 at 14:56

1 Answer 1

3

Loop over the whole featureset array, use Where-Object to filter:

$output = $jsn.abc.comp1.featureset |Where-Object enable -eq $true |ForEach-Object {
   $_.Name
}
Sign up to request clarification or add additional context in comments.

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.