ConvertFrom-Json Player

I am new to PowerShell and have the following code:

$jsonResponse = @" { "departments":[{"id":81,"department":"Sales"},{"id":61,"department":"IT Support"}] } "@ $myCustomObject = $jsonResponse | ConvertFrom-Json $myCustomObject.departments.department[0] $myCustomObject.departments.department[1] 

Lets me access customObject elements (converted from JSON).

I need the ability to scroll an object so that I can access each element i.e.

 object_loop { $myCustomObject.departments.department[x] } 

where x is the increment of the loop.

Sorry, this is a stupid question, but I have googled and can't find a simple example.

Cheers for any help.

Duncs

+6
source share
1 answer

It is as trivial as

 foreach($obj in $myCustomObject.departments) { Write-Host ("Got" + $obj.department) } 
+9
source

Source: https://habr.com/ru/post/985790/


All Articles