简体   繁体   中英

Powershell Json Array content from Azure DevOps Api

    {
"count":  3,
"value":  [
              {
                  "id":  "12345678-123456-23424-123456ff2",
                  "name":  "TestProject",
                  "description":  "Test project  migration",
                  "url":  "https://dev.azure.com",
                  "state":  "wellFormed",
                  "revision":  6619,
                  "visibility":  "private",
                  "lastUpdateTime":  "2019-10-14T06:10:03.557Z"
              },
              {
                  "id":  "12345678-123456-23424-123456ff2",
                  "name":  "KC-TestAutomation-Framework",
                  "description":  "Test Automation Frameworks",
                  "url":  "https://dev.azure.com",
                  "state":  "wellFormed",
                  "revision":  6502,
                  "visibility":  "private",
                  "lastUpdateTime":  "2019-10-03T07:53:33.95Z"
              },
              {
                  "id":  "b2345678-123456-23424-12345",
                  "name":  "Training",
                  "description":  "Training Management Project",
                  "url":  "https://dev.azure.com",
                  "state":  "wellFormed",
                  "revision":  7124,
                  "visibility":  "private",
                  "lastUpdateTime":  "2019-12-02T07:19:24Z"
              }
     ]
}

I've this Json, I need to create from this json another json containing the name,and ID of every project in this form name:name value:id in json file...

I've tried somenthing like this..

   $json = Get-Content $file | ConvertFrom-Json 

   ForEach($i in $json.value.id)
    {
      Write-Host "Id: $($i)","name: $($json.value.name)"
    }
  • Read the JSON file and convert it using ConvertFrom-Json .
  • Select the id and name properties and convert back to json
  • Write the newly created json to file

Something like this:

$json = Get-Content -Path 'X:\YourJsonFile.json' | ConvertFrom-Json

$newJson = $json.value | Select-Object id, name | ConvertTo-Json

# output on screen
$newJson

# output to new json file
$newJson | Set-Content -Path 'X:\YourNewJsonFile.json'

Output:

 [ { "id": "12345678-123456-23424-123456ff2", "name": "TestProject" }, { "id": "12345678-123456-23424-123456ff2", "name": "KC-TestAutomation-Framework" }, { "id": "b2345678-123456-23424-12345", "name": "Training" } ]

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