2

I populated an array using the following:

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

I have the following data populated in an Array in PowerShell:

$jsonDataArray | foreach{Write-Host $_}
@{timezone=America/New_York; city_name=Azalea Park; lon=-81.3004; data=System.Object[]; state_code=FL; country_code=US; lat=28.5535}

I need the data located in each array piece. Particularly: data=System.Object.

I had thought to use the following structure:

$foreach (system.object['data'] in $jsonDataArray)
{
    //pull and extract all data as string objects and populate those strings into a new array. Search each array string and pull data to create histogram.

}

I have looked through code. Does anyone have an idea how I can achieve this data extraction?

0

1 Answer 1

3

Don't let the type of the data field distract you :)

foreach($jsonData in $jsonDataArray)
{
    foreach($data in $jsonData.data)
    {
        # $data represents each object in the data array
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok so - I did a test with the following: foreach ($jsonData in $jsonDataArray) { foreach($data in $jsonDataArray) { Write-Host $data } } and the output still shows as @{timezone=America/New_York; city_name=Druid Hills; lon=-84.3604; data=System.Object[]; state_code=GA; country_code=US; lat=33.8181} . In this case data = System.Object and thats the data I need. So I am not sure how to extract each object and pull that data out.
@WT Notice in my example, the inner foreach loop iterates over $jsonData.data, not $jsonDataArray
Oh wow! Thank you so much Mathias. I really appreciate the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.