locked
API Call Data to Array RRS feed

  • Question

  • I have the following Array. I am leveraging a call against an API and for each time the try and catch goes through, I want to store the json object returned from each call into an array.  

    $jsonDataArray = @()

    forEach ($ip in $ipReturn.IPAddress) {
    try {
    $jsonObject = Invoke-RestMethod -Method Get -Uri "https://api.weatherbit.io/v2.0/forecast/daily?ip=$ipReturn&days=2&units=I&key=$apiKey" -Headers $header
    $jsonDataArray += $jsonObject
    }
    catch {
    $errorTrap = $error[0]
    Write-Host "Status Code:" $_.Exception
    Write-Host "Status Description:" $_.Exception
    }
    }

    Basically, I want to store each callback json into an array that I can transform the data into a simple histogram.  I am making about 75 calls for this loop. Sorry for the silly question, but I am just really learning PS and how to do some more advanced stuff. I can return JsonData Array with the following value : 

    timezone     : America/New_York
    city_name    : Mount Vernon
    lon          : -73.8346
    data         : {@{wind_cdir=WSW; rh=42; wind_spd=11.2; pop=15; wind_cdir_full=west-southwest;
                   slp=1016.4; app_max_temp=59.9; pres=1013.68; ts=1522584000; dewpt=29.5; snow=0; uv=1.1;
                   app_min_temp=45.2; wind_dir=239; weather=; snow_depth=0; max_temp=59.9; precip=0;
                   max_dhi=388.2; datetime=2018-04-01; temp=51.8; min_temp=45.2; clouds=82; vis=6.2},
                   @{wind_cdir=SE; rh=60; wind_spd=6.7; pop=50; wind_cdir_full=southeast; slp=1019.9;
                   app_max_temp=52.7; pres=1017.68; ts=1522670400; dewpt=28.2; snow=0; uv=5;
                   app_min_temp=28.5; wind_dir=134; weather=; snow_depth=0; max_temp=52.7; precip=0.5;
                   max_dhi=505.3; datetime=2018-04-02; temp=41; min_temp=34.4; clouds=63; vis=2.5}}
    state_code   : NY
    country_code : US
    lat          : 40.9083

    timezone     : America/Los_Angeles
    city_name    : Menlo Park
    lon          : -122.1781
    data         : {@{wind_cdir=WNW; rh=74; wind_spd=4.5; pop=0; wind_cdir_full=west-northwest; slp=1014;
                   app_max_temp=69.8; pres=1014.43; ts=1522584000; dewpt=50.4; snow=0; uv=7.9;
                   app_min_temp=51.8; wind_dir=295; weather=; snow_depth=0; max_temp=69.8; precip=0;
                   max_dhi=786; datetime=2018-04-01; temp=58.5; min_temp=51.8; clouds=19; vis=6.2},
                   @{wind_cdir=NW; rh=71; wind_spd=6.7; pop=0; wind_cdir_full=northwest; slp=1013.9;
                   app_max_temp=68; pres=1014.03; ts=1522670400; dewpt=47.5; snow=0; uv=7.2;
                   app_min_temp=51.8; wind_dir=319; weather=; snow_depth=0; max_temp=68; precip=0;
                   max_dhi=711.1; datetime=2018-04-02; temp=56.7; min_temp=51.8; clouds=31; vis=6.2}}
    state_code   : CA

    My question is how do I check that this array is correct? So I can begin writing my histogram code?

    Sunday, April 1, 2018 3:48 PM

Answers

  • $jsonDataArray | foreach{Write-Host $_}  

    That will tell you what is in each part of the array and give output like this:

    @{timezone=America/New_York; city_name=Azalea Park; lon=-81.3004; data=System.Object[]; state_code=FL; country_code=US; lat=28.5535}

    @{timezone=America/Detroit; city_name=Warren; lon=-83.0546; data=System.Object[]; state_code=MI; country_code=US; lat=42.4693}


    • Marked as answer by W T Sunday, April 1, 2018 5:14 PM
    • Edited by W T Sunday, April 1, 2018 5:14 PM
    Sunday, April 1, 2018 5:14 PM