John Miller
John Miller

Reputation: 513

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.

Upvotes: 0

Views: 411

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

Change

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

to

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

Upvotes: 1

Related Questions