0

My spring application does a rest request to a server and the response from the server is a JSONObject string. The JSON string is very huge(200MB). I want to convert the json string to a JSONObject. Below are my code for conversion:

exchange = restTemplate.exchange(Url, HttpMethod.POST, postEntity, String.class);
jsonObject = objectMapper.readValue(exchange.getBody(), JSONObject.class);

For a single request, it is taking 3-5 seconds for conversion. But, if there are multiple requests the conversion is taking so much time (60 seconds for 8-10 requests in parallel). Is there any better way to do this?

Prog_G
  • 1,539
  • 1
  • 8
  • 22

1 Answers1

0

I'd say that transforming a 200MB chunk of JSON to an object using jackson-databind's ObjectMapper will almost always consume a lot of computing time and furthermore huge amounts of memory.

If you don't need the whole object represented by the JSON in memory at a single time, i. e. chunks of it will suffice, I would advise to switch to an approach that utilizes jackson's streaming API. You can combine it with databind on smaller subsets of the JSON, passing the resulting DTOs to some consumer (kind of visitor pattern).

I hope this is of any help for your special use-case.

Timor
  • 127
  • 6