Raviteja Atchuta
Raviteja Atchuta

Reputation: 5

ConvertFrom-Json : Invalid JSON primitive:

I am trying to Fetch the Projects list from ADO using MyPAT and Powershell script as below:

$token = "PAT"
$url="https://dev.azure.com/adient/_apis/projects?api-version=4.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
$Projects = $response.value.name
ForEach( $project in $Projects) 
{
  $url1="https://dev.azure.com/adient/_apis/projects/$($project)?includeCapabilities=true&includeHistory=true&api-version=4.1"
  $response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
  
  Write-Host "result= $($response1 | ConvertTo-Json -Depth 100)"
  
 
}
$ActiveProjects = $response1 | ConvertFrom-Json

$ActiveProjects | FT

I am getting an error stating ConvertFrom-Json : Invalid JSON primitive: . At line:15 char:32

Can anyone please help me fetch the values of the JSON and wanted to convert some data from the JSON into an excel ?

Upvotes: 0

Views: 897

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8254

Based on the sample you have provided over comments below is something that you can try to achieve your requirement.

  • I have concatenated all the Json Objects into array and then used | ConvertFrom-Json.
  • Since your requirement is to have only limited properties, I have used Select-Object and provided id, name and Last Update Time.
  • Lastly, I have exported the file as csv.

Below is the complete script that worked for me.

$projects = $project1,$project2,$project3
$result = '['

ForEach($project in $projects) 
{
    $result=$result+$project+','
}

$result = $result.Substring(0, $result.Length - 1) +']' 
$result = $result | ConvertFrom-Json | Select-Object id,name,'Last updatetime'| Export-Csv -Path "merged.csv"

Results:

enter image description here

Upvotes: 0

Related Questions