Forum Discussion
RichardP
8 years agoHelper I
Stopping when looping through a paginated API feed
Hi everyone, I've been reading up on the advice for looping through a paginated API as a source (great resources here, here and here). I have a slightly different case which I'm trying to dea...
- 8 years ago
Anonymous
3 years agoNot applicable
My solution:
Function "Get JSON" (which actually returns a list)
let
Source = (page as number) as list =>
let
URL = Text.Combine({
"https://some.url&page=",
Number.ToText(page)
}),
Response = Web.Contents(
URL
),
Source = Json.Document(Response)
in
Source
in
SourceQuery
let
Source = List.Combine(
List.Generate(
() => [page = 1, Func=#"Get JSON"(1)], // initial
each List.Count([Func]) > 0, // condition
each [
page = [page]+1,
Func = #"Get JSON"([page])
], // next
each [Func] // selector
)
),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
in
#"Converted to Table"It starts calling the "Get JSON" function starting with page=1 and going as long as the function returns at least 1 item.