Jump to content

Recommended Posts

Hello,

I am trying to download to our CRM all messages from Gmail account using the Gmail API.

I have to do a first call to the API to get the first 1-100 messages IDs. With that call I get a nextPageToken that I can add to the url if I make a second call in order to get messages IDs from 101-200. So each time I make the call from Import from URL I get a $json in with the structure of:

{   "messages": [     users.messages Resource   ],

"nextPageToken": string,  

"resultSizeEstimate": unsigned integer }

where [     users.messages Resource   ] is a json array in the form of  from [{"id" : "1" , "threadId":"1"} , ... , {"id" : "100" , "threadId":"100"}]

My next step is to set variable $json1 = ( JsonGetElement ( $json ; "messages" ) and with that I get a Json Array of all the messages IDs.

My problem starts from the second call, once I get the following 100 messages ids.

When I make the second call and I get the next list of 100 messages ids and set variable $json2 = ( JsonGetElement ( $json ; "messages" )

Then I need to join the two variables (json arrays) into one that I will later loop and get each messages details.

However, I am not able to understand how to properly write the  JsonSetElement function so it can join $json1 array and $json2 array into a third $json3 array that would contain all messages ids.

Thanks a lot in advance for any suggestion on how to write the function.

Luis

Link to post
Share on other sites

I wouldn't try to keep one array of message IDs; I would probably loop and parse the first 100 and then move on to the next.  Or if you want to postpone the parsing and processing, just create an array of json objects where each item in the array is is just the json. you received.  Then when you get to parsing you do an outer loop through your array and an inner loop through the messages in each array element.

Link to post
Share on other sites

Your array contains a set of objects. I don't know of a way to select a group of objects in order to add them to another array. You can either select the array itself or a specific single object. Of course, you could loop over the objects and add them one-by-one - but that wouldn't be very useful when your ultimate purpose it to loop over them in order to parse out their data.

I would suggest you index your variables using the repetition number as $json[1], $json[2], etc. instead of naming them $json1, $json2. That will make it easy to loop over them in an outer loop and parse their objects in an inner loop, with no need to combine them beforehand.

 

 

Edited by comment
Link to post
Share on other sites

The downside in storing them in separate variables or in separate variable repetitions is that they become difficult to pass around to subscripts.  If you use one variable and you create an array of your objects, you have something that is both easy to iterate over and easy to pass around.

Link to post
Share on other sites
50 minutes ago, Wim Decorte said:

The downside in storing them in separate variables or in separate variable repetitions is that they become difficult to pass around to subscripts

 I don't see that. Unless you want to pass them en bloc - and why would you want to do that.

Link to post
Share on other sites

Plenty of reasons why you'd want to do that, mainly having to do with not wanting to loop through the results in the script that gets the response from the API.

My comment has a general design-pattern note to it: it's something that I see often in solutions that I review and developers that I mentor: storing data in separate variables or separate repetitions can be a hurdle to refactoring some of the logic.  And there is an easy solution with the arrays and json capabilities that we now have natively.

Link to post
Share on other sites
16 minutes ago, Wim Decorte said:

not wanting to loop through the results in the script that gets the response from the API.

In such case you would simply call the other script within the outer loop.

 My point - my entire point - is that it is  a waste of time and effort to try and combine the groups into one giant supergroup when the goal is to process the individual objects one-by-one. If you like, you can cal it a general design-pattern too. I just call it common sense.

 

Link to post
Share on other sites

Agreed on your point of not wasting effort in parsing each object for the purpose of adding its elements to another object.  But my point is somewhat different, since I'm not talking about building a super group, merely an array of groups.  All it takes is one JsonSetElement() call each time you get an API response with a page of emails.

When it is time for the processing, you'd still be processing each object individually as you retrieve each one from that array.  That's the beauty of arrays: each element can be a complex object in its own right.

Code-wise your approach and mine works exactly the same way; but instead of setting a new variable (or rep) you'd just add to the same one.  The benefit is that in my approach I can pass the result en-bloc to another script without having to do any extra work.

Link to post
Share on other sites

The point we disagree on is not how to send it en bloc, but whether to send it en bloc. If you prefer to place the parsing logic in a sub-script, then I would advise to do what you suggested right from the start:

16 hours ago, Wim Decorte said:

I would probably loop and parse the first 100 and then move on to the next.

In such case, there is no need for variables nor an array of arrays. You just call the subscript with a parameter of the current array to be parsed right from the initial loop that gets the data (I presume it's a loop too).

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Similar Content

    • By dancer5678
      I am using Filemaker Server 18 on Windows Server 2012 R2
      Been using it for years with no issues
      Currently when I log in to the console it is very sluggish.
      When I get to the Dashboard it shows No databases, then it auto refreshes and the database list appears.
      Within 15 seconds of scrolling the database list to open files the screen refreshes. This situations is happening over and over in a loop.
      Any Thoughts on what is causing this issue?
       
       
       
       
    • By stevaroni
      I get an error 3 when using a script to Export Records via WebDirect. Using FileMaker Server 18 and have tried both Safari and Chrome both with same results. I have tried using the temporary path, desktop path, and documents path. I have tried using with the automatically open and not. I have tried writing a tab delimited and comma delimited file. Does anyone have ideas I haven't yet tried?
    • By cheerfulzebra
      Hi All
       
      I am hoping some one can help!
      I have a table called TPM, in that table is a field "Next Due Date" Which is auto generated by creating a new record. You input the date you did the service and then it adds 3 months to that date and that appears in the Next Due Date Field.. All working...
       
      What I am trying to do is when the database loads it runs a script to see if there are any outstanding TPM Services to do...
      Here is the script
      Show Customer Dialog
      Message: If ( TPM::TPM Next Due Date < Get (CurrentDate ) ; "You have outstanding TPM services" ; "You have no outstanding services")
      I thought it would be that simple. So I created a TPM Due date Less that Current date and ran the script...."You have outstanding TPM Services"...GREAT! Then I deleted that record and ran the script again...""You have outstanding TPM Services" Oh!!! That isnt true!
       
      PLEASE HELP I'M LOOSING THE WILL TO LIVE HERE!!!
    • By Kitesurfer
      Hi,
      Can someone please explain why Leftwords function is not properly working when importing an excell sheet.
      For example the name John Doe LeftWords ( Client ; 1 ) should show "John" only.
      When typing John Doe it works fine but not when importing ????
      Looking forward to a reply.
      Kind regards
      Rudy
    • By Pio Soto
      Hi,
      My issue is that my customer layout has a script trigger on LayOutEnter that sorts my customers portal list by "Active" customers,
      but when I do a go to related record from another table say from quotes or any other the customer layout does not load that customer
      or show my related customer because the OnLayoutEnter script trigger changes it, I would like it to go to the related customer and also sort it by "Active"
      on the list.
      Hope someone can help me with this.
      Thanks,
      Pio
  • Who Viewed the Topic

    1 member has viewed this topic:
    WF7A 

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.