John Miller February 2016

How to display JSON output?

First API to call:

https://proto123.hipchat.com/v2/user?auth_token=2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I get the user IDs when I run this URI.

Then I need to execute the next URI for each user ID generated from the previous URI:

https://proto123.hipchat.com/v2/user/id?auth_token=2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then print the output file to CSV.

How do I Do that?

Please check this script:

$uri = "https://proto123.hipchat.com/v2/user?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
try {
  $webResponse = Invoke-RestMethod -uri $uri -ContentType "application/json"
} catch {
  $webResponse = $_.Exception.Response
}

if ($webResponse) {
  Write-Debug "Output result"

  $webResponseIDList = @() # Defining array for collecting the ID's
  foreach ($webResponseID in $webResponse) {
    $webResponseIDList += [PSCustomObject] @{
      id = $webResponseID.id
    } # End of ID's Array
    $id = $webResponseID.id

    Write-Host "User ID is : $id" + $webResponseIDList `r`n
    $uri = "https://proto123.hipchat.com/v2/user/$id?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
    $webResponseID = Invoke-RestMethod -uri $uri -ContentType "application/json"

    Write-Host "User ID are :" + $webResponseID.id `r`n
    Write-Output "User ID are :" + $webResponseID.id `r`n
  }
} else {
  Write-Debug "No results were returned"
}

I am not getting the output in PowerShell.

Answers


Ansgar Wiechers February 2016

Change

foreach ($webResponseID in $webResponse) {
  ...
}

to

foreach ($webResponseID in $webResponse.items) {
  ...
}

Post Status

Asked in February 2016
Viewed 1,846 times
Voted 8
Answered 1 times

Search




Leave an answer