将数据存储在内存中并作为json传递

My objective is to collect data from a webservice and deliver it to a client as a json. The problem is sometimes the query is just to big to store into an array and because of that im facing problems with memory exaustion.

To complete the query i need to send multiple requests to the webservice and as the data arrives i append it into an array.

When all the process finishes i return the array as a json to the user.

Im studying other ways of doing this:

  1. Judy array - my structure is really big and i managed to save 50% of the memory usage with this data structure. The problem now is returning a json object from this structure in a efficient way.
  2. Setting a variable and append encoded json to it. (guessing that json is much more lightweight than a conventional array).

Any more suggestions?