0

I have an API that gives a response like this :

{
 responseTime: "12 mls",
 totalCount : "1000",
 offset:"0",
 limit:"1000",
 ... // other json properties
 items: [
  {entity1.Property1 = value1, entity1.Property2 = Value 2, ...},
  {entity2.Property1 = value1, entity2.Property2 = Value 2, ...},
  .... // 1000 enities
 ]
}

The entity inside the array is very big and the whole response json can go over 200 MB easily. I am wondering how may I consume the array inside the API response as an iterator, each element one by one.

I found an aswer on how I can iterate through an api response that has only an array of elements: {results: [{},{}, ...]} (asnwer here) , but I am not sure how to apply that on a response that has other properties other than the array itself.

Clarification: I want to firstly read the properties of the json (totalCount,limit and offset ) and then read one entity object from the stream at a time and deserialize it to MyEntityModel.

Any idea is welcomed!

SimpForJS
  • 88
  • 9

1 Answers1

0

I think that the real problem is not in the way you should do the iteration. You can do this with a simple for or foreach loop.

The problem lies in the fact that the API spits out over 200 mb of data for a single request.

If the API is not under your control, maybe there is a way that you can filter the results in the request that you are making to the API?

Mekroebo
  • 352
  • 4
  • 11
  • I know how to use a foreach thanks. The API is not under my control and your answer doesn't help one bit. The API response is supposed to be like that. I know it is not a good practice (the 200 +MB ) , but it's not in my power. That is the size with the filters applied. I don't want to get into that. My question was how do I use the stream reader to get the json properties and then iterate over the array inside the json without reading everything at once to memory. Basically I want to read that response as performant as possible without changing the API code or the size of its response. – SimpForJS Apr 20 '22 at 17:31