Combine API results into single object

Welcome Forums General PowerShell Q&A Combine API results into single object

Viewing 7 reply threads
  • Author
    Posts
    • #263615
      Participant
      Topics: 1
      Replies: 4
      Points: 15
      Rank: Member

      I have a process to interact with an API and write the data to a SQL table. Hitting the two endpoints separately and getting the data is no issue. I am stuck now on how to combine them so I can make one write to SQL as the loop processes. Endpoint #1 gets me all the data “/api/v1/employee/$($_.eecode)” . I need to hit Endpoint #2 “/api/v1/employee/$($_.eecode)/customfield”

      with the same eecode from the first call. I want to keep them in the same object so I can write the row to SQL with data from both endpoints. I had to remove some code since the API for the system I am interacting with isn’t public.

      • This topic was modified 1 week, 3 days ago by kvprasoon. Reason: code formatting
    • #263645
      Participant
      Topics: 12
      Replies: 541
      Points: 1,314
      Helping Hand
      Rank: Community Hero
      • This reply was modified 1 week, 4 days ago by Sam Boutros.
    • #263660
      Participant
      Topics: 1
      Replies: 4
      Points: 15
      Rank: Member

      Sam, I never would have thought of putting the call in the expression for ADID. I added CustomText04.value and by itself, it returns the value I would need. Now I am getting an error when the second call is being made and selecting the $params.

      <!–StartFragment –>

      Error:
      <!–StartFragment –>

      … -apiEndpoint “/api/v1/employee/$($_.eecode)” | Select-Object $params
      | ~~~~~~~~~~~~~~~~~~~~~
      | The “expression” key cannot have an empty string value.

      <!–EndFragment –>

      <!–EndFragment –>

       

    • #263699
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      The expression key value needs to be a script block (surrounded by {}).

    • #263705
      Participant
      Topics: 1
      Replies: 4
      Points: 15
      Rank: Member

      Thanks! That helped out getting rid of that error. My text file I write out is still blank for the ADID. Running the expression by itself and getting the value of the custom field gets me what I want.

      I can also grab the value directly and be left with a string.

      If I load the expression into my shell. The array looks as if it isn’t executing the command?

       

    • #263714
      Participant
      Topics: 12
      Replies: 541
      Points: 1,314
      Helping Hand
      Rank: Community Hero
    • #263909
      Participant
      Topics: 1
      Replies: 4
      Points: 15
      Rank: Member

      I have the expression fixed with the extra {} to clear my error. When I take out the variable and hard code an employee code it will execute and stick the value into the $params array.

      However, when I have the variable in the execution it comes out blank in my text file. I know the variable evaluates correctly as I can write the foreach-object and get all the values in my shell.

    • #264014
      Participant
      Topics: 1
      Replies: 4
      Points: 15
      Rank: Member

      I updated the code with my final result. I refactored a bit, but have the result I need now to make the SQL table write. Thx for your input. It helped out a lot.

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