简体   繁体   中英

Read JSON file through PowerShell

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]

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM