-1

I need list of value which change_flag is TRUE.

In the list, I need eg "ZIPZOOMBIN_VERSION", "KERNEL_VERSION" which I am using as an array list in another PowerShell file

{
  "ZIPZOOMBIN_VERSION": {
    "change_flag": "TRUE",
    "localpath": " "
  },
  "KERNEL_VERSION": {
    "change_flag": "TRUE",
    "localpath": " "
  },
  "ACTIVE_MQ": {
    "change_flag": "FALSE",
    "localpath": " "
  }
}

list = [ZIPZOOMBIN_VERSION, KERNEL_VERSION]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jugal Shah
  • 13
  • 5
  • Do you have any influence on the created json? I think it should be designed as an array of elements that have three subelements (name, change_flag, localpath). Then it would be way easier to evaluate :-) – stackprotector Apr 28 '20 at 06:26
  • No I do not have control.I need to consume json – Jugal Shah Apr 28 '20 at 06:44

1 Answers1

0

The key is to iterate over the properties of an object, that you do not know in advance. You can read more about it in this question. You can do it like this:

$content = @"
{
  "ZIPZOOMBIN_VERSION": {
    "change_flag": "TRUE",
    "localpath": " "
  },
  "KERNEL_VERSION": {
    "change_flag": "TRUE",
    "localpath": " "
  },
  "ACTIVE_MQ": {
    "change_flag": "FALSE",
    "localpath": " "
  }
}
"@
$myObject = ConvertFrom-Json -InputObject $content
$myList = @()
foreach ($property in $myObject.PSObject.Properties) {
    if ($property.Value.change_flag -ilike "TRUE") {
        $myList += $property.Name
    }
}
$myList

The content of $myList will be the two strings ZIPZOOMBIN_VERSION and KERNEL_VERSION.

stackprotector
  • 10,498
  • 4
  • 35
  • 64