Home > Enterprise >  powersehll JSON parse to get property name
powersehll JSON parse to get property name

Time:10-30

Need to parse this JSON file

{
  "domains": {
    "1.com": {
      "data_path": "c:\\zzzzz\\Domains\\1.com"
    },
    "22.com": {
      "data_path": "c:\\zzzzz\\Domains\\22.com"
    },
    "333.com": {
      "data_path": "c:\\zzzzz\\Domains\\333.com"
    },
    "4444.com": {
      "data_path": "c:\\zzzzz\\Domains\\4444.com"
    }
  },
  "domain_aliases": {}
}

To get the domain properties values, like

1.com 22.com 333.com 4444.com

Thanks

CodePudding user response:

Use ConvertFrom-Json to parse the json:

$json = @'
{
  "domains": {
    "1.com": {
      "data_path": "c:\\zzzzz\\Domains\\1.com"
    },
    "22.com": {
      "data_path": "c:\\zzzzz\\Domains\\22.com"
    },
    "333.com": {
      "data_path": "c:\\zzzzz\\Domains\\333.com"
    },
    "4444.com": {
      "data_path": "c:\\zzzzz\\Domains\\4444.com"
    }
  },
  "domain_aliases": {}
}
'@

$data = $json |ConvertFrom-Json

Then reference the domains property and use the psobject hidden memberset to enumerate its properties:

$data.domains.psobject.Properties |ForEach-Object Name
  • Related