Cyber.Drunk
Cyber.Drunk

Reputation: 165

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)"
    }

Upvotes: 1

Views: 511

Answers (1)

Theo
Theo

Reputation: 61028

  • 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"
    }
]

Upvotes: 2

Related Questions