Working with API results in PowerShell

Welcome Forums General PowerShell Q&A Working with API results in PowerShell

Viewing 8 reply threads
  • Author
    Posts
    • #266966
      Blocked
      Topics: 1
      Replies: 4
      Points: -17
      Rank: Member

      So I definitely still consider myself a beginner when it comes to PowerShell but I like to dive right in. I’m sure this has been asked before but I guess I don’t know enough to search for the right thing. I usually am able to find something someone has written and modify it to my needs but not this time. So lets get into what my question is… so initial info…

      This is pretty much as far as I’ve gotten. I can get the results but I’m lost on how to work the results in meaningful way.

      I can use $field.hosts and get results to the host screen but I can’t export/save them to a file or anything else.

      an example of the results of $field.hosts

      PS C:\Users\use> $field.hosts

      id metadata
      — ——–
      111 {@{categoryId=4109c230-a90a-4b2c-a0c6-ae6b33e76ec1; valueId=9e05ac7c-0aa5-4521-bc15-b96044b52639}, @{categoryId=80955841-ea5c-4d12-b32e-…
      112 {@{categoryId=4109c230-a90a-4b2c-a0c6-ae6b33e76ec1; valueId=b7dae04d-3792-49aa-b393-f5a36341f94f}, @{categoryId=83d42353-f657-47ce-9a9b-…
      113 {@{categoryId=4109c230-a90a-4b2c-a0c6-ae6b33e76ec1; valueId=b47d11e8-46e5-4545-bdc1-7d232579de56}, @{categoryId=80955841-ea5c-4d12-b32e-…

      I used

      That should give you an idea of what it looks like.

      In the end I’ll have to compare valueId info with another api result and then eventually export that valueId based on the overall ID to a new csv property/column of the associated ID. For right now I am just trying to figure out how to convert this to a usable format. Hope I made sense… Off to sleep.

      • This topic was modified 1 month ago by dugle8038.
      • This topic was modified 1 month ago by dugle8038.
      • This topic was modified 1 month ago by kvprasoon. Reason: code formatting
      • This topic was modified 1 month ago by kvprasoon. Reason: correct title
    • #266996
      Senior Moderator
      Topics: 9
      Replies: 1420
      Points: 5,280
      Helping Hand
      Rank: Community MVP

      Not really sure how you are going to compare the result. But you can use Import-CliXML cmdlet to read the exported object.
      Can you elaborate the problem a little more ?

    • #267059
      Blocked
      Topics: 1
      Replies: 4
      Points: -17
      Rank: Member

      I’m not sure how I’m going to get there yet either. One step at a time. Right now I can’t work with the metadata. From what I read I think I need to convert metadata to a string. I’ve seen examples of how to do that if you are populating the pscustomobject yourself but not if you are populating it from another source(api).

      Right now I can do

      But if I try the same on a categoryId or ValueId, it returns nothing.

    • #267083
      Participant
      Topics: 16
      Replies: 1855
      Points: 3,568
      Helping Hand
      Rank: Community Hero

      When you are using Invoke-RestMethod, Powershell will convert the JSON into a Powershell object. JSON is nested data, so the assumption is that you want to flatten the data. You have one id that has many metadata. If you want to make it flat, you could do something like this:

    • #267086
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      Seems like it would be

    • #267140
      Blocked
      Topics: 1
      Replies: 4
      Points: -17
      Rank: Member

      Thanks Rob, That is what I was looking for

    • #267161
      Participant
      Topics: 16
      Replies: 1855
      Points: 3,568
      Helping Hand
      Rank: Community Hero

      Another method worth mentioning is calculated expressions based on dot notation that Doug suggested

    • #267197
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      No no no, yours was much better. 🙂

    • #267215
      Blocked
      Topics: 1
      Replies: 4
      Points: -17
      Rank: Member

      Thanks to both of you. That got me over the hump to a working script. I’m sure it could be written better but it does the job.

      Here is the rest of it if you were curious.

Viewing 8 reply threads
  • You must be logged in to reply to this topic.