Search code examples
jsonparsingtext-parsing

How to select words after /slash from a json file in a powershell script


I've a json file from a az cli command, I want to deploy with a powershell script from a tool like jenkins or rundeck, I need to take the field "name" from json but not entirely only the word after forward slash

    [
  {
    "Group": "KC-EMEA-RSGP-BPTRAINING-01",
    "id": "/subscriptions/KC-EMEA-RSGP-BPTRAINING-01/providers/Microsoft.Sql/servers/",
    "name": "kc-emea-sqsrv-bptraining-dev-01/master"
  },
  {
    "Group": "KC-EMEA-RSGP-NAVISIONKM-DEV-01",
    "id": "/subscriptions/KC-EMEA-RSGP-NAVISIONKM-DEV-01/providers/Microsoft.Sql/servers/",
    "name": "km-emea-sqsrv-navision-tst-01/km-emea-sqdb-navision-tst-01"
  },
  {
    "Group": "KC-EMEA-RSGP-NAVISIONKM-DEV-01",
    "id": "/subscriptions/KC-EMEA-RSGP-NAVISIONKM-DEV-01/providers/Microsoft.Sql/servers/",
    "name": "km-emea-sqsrv-navision-tst-01/master"
  },
  {
    "Group": "KC-EMEA-RSGP-PROJECTS-DEV-01",
    "id": "/subscriptions/KC-EMEA-RSGP-PROJECTS-DEV-01/providers/Microsoft.Sql/servers/",
    "name": "kc-emea-sqsrv-projects-dev-01/KC-EMEA-SQDB-BPTRAINING-TRAINEE-01"
  }
]



$file = "pathtojsonfile.json"
$jsonContent = Get-Content $file | ConvertFrom-Json;

$namedb = $jsonContent.name;
$value= $namedb.Substring($namedb.IndexOf('/')+1) --> this doesn't work.


Solution

  • Try $namedb[0].Substring($namedb[0].IndexOf('/')+1)
    And for all of them:
    foreach($name in $namedb) {$name.Substring($name.IndexOf('/')+1)}