Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

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"
}

Open in new window

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
}

Open in new window

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 } }

Open in new window

To do the pull in $ListB... but the result I got is wrong... some data got inside other place. Any idea?

Avatar of oBdA
oBdA

Doesn't seem like you have two lists. You seem to have one list from the first resource URL, and the second resource URL provides details from a specific item in the first list.
Try it like this:
$list = getrestAPI -Url "Link" 

$list | ForEach-Object {
	$row = $_
	$details = getrestAPI -Url "Link.com?=$($row.ID)"
	[PSCustomObject]([ordered]@{
		ID = $row.ID
		Name = $row.Name
		Address = $row.Address
		Item = $details.Item
		PhoneNumber = $details.PhoneNumber
	})
}

Open in new window

Avatar of WeTi

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...

I don't quite understand that setup; why would you get two different "Item"s for the same ID?
Avatar of WeTi

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 CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

Yes, I will try now thanks.

Avatar of WeTi

ASKER

Just checked the result... the $ListC is correct but $ListB became a loop

Sorry, but I have no idea what you mean with "$ListB became a loop".
Avatar of WeTi

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. 

No, it didn't. This has nothing to do with the loop.
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
Avatar of WeTi

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... 

Avatar of WeTi

ASKER

Ok I understand now, the listB data is wrong, I need to rethink the structure. Thanks alot