0

I am trying to get data from a JSON file and loop through its contents...

I try this ...

$webData = Invoke-WebRequest "http://myApp?$select=FirstName,LastName,JobTitle,SAMAccountName,Department"

$thePeople = ConvertFrom-Json $webData.content

write-host $thePeople | Get-MemberPS C:\Users\dev> C:\SPO\code\PowerShell Scripts\getData.ps1

I get this error

@{odata.metadata=http://myApp/View/odata/$metadata#PeopleDepartmentOData; value=System.Object[]}

Get-Member : You must specify an object for the Get-Member cmdlet.

At C:\SPO\code\PowerShell Scripts\getData.ps1:7 char:25

+ write-host $thePeople | Get-Member

+                         ~~~~~~~~~~

    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException

    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

The JSON file is ...

{

  "odata.metadata":"http://myApp/View/odata/$metadata#PeopleDepartmentOData&$select=FirstName,LastName,JobTitle,SAMAccountName,Department","value":[

    {

      "FirstName":"Mark","LastName":"Johnson","JobTitle":"Director of 

stuff","SAMAccountName":"johnsonm","Department":"Head Office"

    },{

      "FirstName":"Conor","LastName":"Con","JobTitle":"Senior Guy","SAMAccountName":"cc","Department":"Corporate"

    },{...

 

Thanks P

Pete Whelan
  • 85
  • 11
  • 2
    `Write-Host` bypasses the pipeline and writes straight to the screen instead. Do `$thePeople |Get-Member` without the `Write-Host` statement, or nest it in a separate pipeline (`Write-Host ($thePeople |Get-Member)`) to make it work – Mathias R. Jessen Nov 25 '21 at 15:18
  • Thanks. And do you know how I would loop through and get each persons first and surname? thanks p – Pete Whelan Nov 25 '21 at 15:20
  • 1
    Based on the JSON snippets you've shown `$thePeople.value` should contain an array of obejcts, so you'll want something like `$thePeople.value |ForEach-Object { "FirstName is $($_.FirstName)"}` – Mathias R. Jessen Nov 25 '21 at 15:22

0 Answers0