[Resolved] Process JSON on second pass

Need some help and/or ideas. I am calling an API. Get back a JSON object of Products with nested arrays - 25 products in all. I can only process 10 on the first pass and display the data in FB Messenger. The subscriber will have a NEXT button. they press the NEXT button I need to get the next 10 products.

I have this working fine when data coming from MYSQL - just use offset and query again plus I store the total number of products. How would I do that with the JSON object? Do I save it to process later? Do I re call the API to get the data again and just skip to the 11th one?

Actually now that I am typing this I could do the same as the SQL table. I get the total # rows and then use an offset of 10. I could do the same here - make a call to the API everytime and then move thru the JSON object.

Any other suggestions?

Often how you approach this is API-specific depending on what functionality the API provides - some allow for offsets, others specify a range or a page number, others the end-point to hit for the next batch. And of course some others make no such provision and leave you to figure it for yourself :slight_smile: One way would be to setup a queue, your calls to the API populate the queue and your app loops through the queue removing each one as it’s processed - push(), pop() and unshift() array functions become handy for this.

2 Likes

Thanks Gareth! I appreciate the ideas!