asked on
Pull data from API and present the data
Dear experts
Im trying to solve this in powershell I dont know how:
$ListA = getrestAPI -Url "Link"
$ListB = getrestAPI -Url "Link .com?=$($ListA.ID)"
in $ListA you need a number to pull the data from $ListB.
in $ListA there are some informations that I need to present.
in $ListB there are some information that I need to present as well.
Here is what I did to try:
$ListA = getrestAPI -Url "Link"
foreach ($ID in $ListA){
$ListB = getrestAPI -Url "Link.com?=$($ID).ID"
}
Here is what I stuck, how do I pull data from $ListA and $ListB?
Lets say I need $ListA.ID, $ListA.Name, $ListA.Adress
And I need $ListB.item, $ListB.phonenumber
Output:
ID: 123455 ($ListA.ID)
Name: Lisa ($ListA.Name)
Adress: Somewhere ($ListA.Adress)
Item: Book of IT (ListB.item)
Phonenumber: 124355523 ($ListB.phonenumber)
ID: 345335 ($ListA.ID)
Name: Maria ($ListA.Name)
Adress: Somewhere ($ListA.Adress)
Item: Book of worms (ListB.item)
Phonenumber: 124445523 ($ListB.phonenumber)
I couldn´t figure it out, how I can add those in foreach loop? The ID should in all the places so this means:
$ListA = getrestAPI -Url "Link"
$ListB = foreach ($ID in $ListA){
getrestAPI -Url "Link.com?=$($ID).ID"
}
$listAresult = foreach ($item in $ListA){
"ID: " + $item.ID
"Name: " + $item.Name
"Adress: "+ $item.Adress
}
$ListBresult = foreach ($item2 in $ListB) {
"Item: " + $item2.Item
"PhoneNumber: " + $item2.Phonenumber
}
Problem with the above code is that how do I put this list together? If I simply doing this:
$ListAresult
$ListBresult
The list could be wrong? I dont know what to do here... I could also do this:
$ListA = getrestAPI -Url "Link"
$ListB = foreach ($ID in $ListA){
getrestAPI -Url "Link.com?=$($ID).ID"
foreach ($item in $ListA){
"ID: " + $item.ID
"Name: " + $item.Name
"Adress: "+ $item.Adress
foreach ($item2 in $ListB) {
"Item: " + $item2.Item
"PhoneNumber: " + $item2.Phonenumber
}
}
To do the pull in $ListB... but the result I got is wrong... some data got inside other place. Any idea?
ASKER
Thanks for answering, the problem im facing now is:
$details return with many information as well, so I need a foreach there as well...
ASKER
obdA my hero I want to say im sorry for the lack of information here is what im doing:
$ListA = $get-restapi -uri listA
$ListB = $get-restapi -uri ListB?=listA.id
$ListC = $get-restapi -uri listC?=listB.id
$ListA and B and C will return alot of informations that I dont really care to know.
$ListA I only need the ID to pull information from $ListB.
$ListB and $ListC those information is what I need to present.
$ListA i need ID
$ListB I need Name and Adress
$ListC I need Item and Phonenumber
Foreach should be at $ListA ID so the output become:
ID: 23445
Name: Lisa
Adress: somewhere
Item: something
Phonenumber: 2045676
ID: 23446
Name: Maria
Adress: somewhere
Item: something
Phonenumber: 2023476
etc...
ASKER
Yes, I will try now thanks.
ASKER
Just checked the result... the $ListC is correct but $ListB became a loop
ASKER
for exemple:
$listC.item
output: "somebook"
$ListC.phonenumber
output: "some number"
is correct.
When Im checking the $listB:
$ListB.Name
Output: Maria, Lisa, Eric etc etc
$ListB.adress
Output: Somewhere, somewhere etc etc
The ListB gathered alla data in foreach and presented it.
It just means that the ID from A returns multiple results when B is queried with that ID.
Is that really expected, and if so, what is supposed to happen?
You can have
1. A single row per "A ID", with arrays in Name (and Address) (what you have now)
ID: 12345
Name: {Maria, Lisa, Eric}
2. A single row per "A ID", with a string in Name (and Address), where the array is joined by some delimiter (this is better suited for export to csv)
ID: 12345
Name: Maria; Lisa; Eric;
3. One separate row per "B ID"
ID: 12345
Name: Maria
-----
ID: 12345
Name: Lisa
-----
ID: 12345
Name: Eric
ASKER
I dont understand you now :)
I just want simply show:
ID: 124534
Name: Lisa
Adress: somewhere
Item: whatever
Phone: 23323232
I tried to pull out: $listB.Name to show what is going on, the Names are in arrays...
ASKER
Ok I understand now, the listB data is wrong, I need to rethink the structure. Thanks alot
Try it like this:
Open in new window