Invoke-WebRequest Working with Json

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of James James 8 months, 2 weeks ago.

  • Author
    Posts
  • #36759
    Profile photo of James
    James
    Participant

    So I have been trying to learn more about working with APIs and wanted to try and connect with the myGetpocket account app API. When I get the content back it seems to be in a Json format and each item received is a new

    System.Management.Automation.PSCustomObject and each article is a NoteProperty. 
     TypeName: System.Management.Automation.PSCustomObject
    
    Results of | Get Member
    Name        MemberType   Definition                                                                                         
    ----        ----------   ----------                                                                                         
    Equals      Method       bool Equals(System.Object obj)                                                                     
    GetHashCode Method       int GetHashCode()                                                                                  
    GetType     Method       type GetType()                                                                                     
    ToString    Method       string ToString()                                                                                  
    1000085475  NoteProperty System.Management.Automation.PSCustomObject 1000085475=@{item_id=1000085475; resolved_id=1000085...
    
    $auturi = 'https://getpocket.com/v3/get'
    $inv = Invoke-RestMethod -Method Post -Uri $auturi -Body $data 
    
    

    So if I run $inv.list in ISE it starts to populate with all of note Properties like 1000085475 and If I choose one then I get the information that I need.
    Like so

    item_id        : 1000085475
    resolved_id    : 1000085475
    given_url      : https://gist.github.com/NickCraver/7ebf9efbfd0c3eab72e9
    given_title    : 
    favorite       : 0
    status         : 0
    time_added     : 1445362357
    time_updated   : 1448913158
    time_read      : 0
    time_favorited : 0
    sort_id        : 187
    resolved_title : (In Progress) PowerShell Script I use to customize my machines in the same way for privacy, search, UI, 
                     etc. · GitHub
    resolved_url   : https://gist.github.com/NickCraver/7ebf9efbfd0c3eab72e9#file-windows10-setup-ps1-L90
    excerpt        : Windows10-Setup.
    is_article     : 1
    is_index       : 0
    has_video      : 0
    has_image      : 0
    word_count     : 712
    
    

    Any clue how I can iterate through all of the objects to create a new object and work with the data as needed ? I have been trying mutiple foreach-object methods and I can't seem to figure it out.

    I have also tried using Convertfrom-Json and Convertto-Json , Export-Clixml for a different way to work with the data.

    Thanks for any help.

  • #36763
    Profile photo of Arie H
    Arie H
    Participant

    Something like this:

    And Trevors latest movie with GitHub as an example, still nice to see how its done.

    https://channel9.msdn.com/Blogs/trevor-powershell/Automating-the-GitHub-REST-API-Using-PowerShell

  • #36764
    Profile photo of James
    James
    Participant

    Thanks for the comment Arie, actually Trevor's Github video is were I got the idea. I will go back and revisit that video and thank you for the other link, much appreciated.

    Here is the documentation: I think the problem is that each article is returned as a separate object.
    https://getpocket.com/developer/docs/v3/retrieve

    data = @{
    contentType = 'article'
    }  | ConvertTo-Json
    
    $auturi = 'https://getpocket.com/v3/get/'
    $Rest =  Invoke-RestMethod -Method Post -Uri $auturi -Body $data -ContentType 'application/json'
    $rest.list.1001374664
    
    
    item_id        : 1001374664
    resolved_id    : 1001374664
    given_url      : http://www.virtualizationhowto.com/2015/07/windows-10-edge-opened-builtin-administrator-account/
    given_title    : 
    favorite       : 0
    status         : 0
    time_added     : 1448414321
    time_updated   : 1448414322
    time_read      : 0
    time_favorited : 0
    sort_id        : 143
    resolved_title : Windows 10 Edge can't be opened using the built-in administrator account
    resolved_url   : http://www.virtualizationhowto.com/2015/07/windows-10-edge-opened-builtin-administrator-account/
    excerpt        : With the release of Windows 10 today, many are spinning up VMs and loading the new workstation on test 
                     boxes to take a look at the final build version. If you are like me, one of the first things I wanted to 
                     test drive was the final release build of the Edge browswer.
    is_article     : 1
    is_index       : 0
    has_video      : 0
    has_image      : 1
    word_count     : 368
    
    
  • #36774
    Profile photo of Arie H
    Arie H
    Participant

    Not necessarily. Your example just asks for a specific item as you pass its number in the request.

    The link you did provide shows a request for a set of items (count=10 and also limited by status) and they mention towards the end that you should use the since and count to limit the returned results, which is logical, http does have its limits on the payload.

    You can most likely get a list of item I'd put that in an object variable or array and then iterate on it with a second Invoke-WebRequest

  • #36784
    Profile photo of James
    James
    Participant

    I wish there was a thanks button. But thanks again Arie, I tried that method as well it resulted in the same thing. It was really more for a learning experience so no biggie. I think I will learn with github and Trevors example.

You must be logged in to reply to this topic.