Jump to content
Server Maintenance This Week. ×

how to join two Json Arrays


This topic is 1459 days old. Please don't post here. Open a new topic instead.

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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

This topic is 1459 days old. Please don't post here. Open a new topic instead.

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
×
×
  • Create New...

Important Information

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