-1

Recently we tried to migrate our existing v1 azure applications to v2 and node version 10+. When we did that we found out that UI application which calls APIS on function app went slow sometimes they killed the browser. When we investigated found out that all size of responses from new version function app was increased like 2.5x times. For small responses, the application worked fine but when it returned a large payload like 10MB+ the application started to freeze. In some cases, it went to 100MB+. The reason why it was so large because JSON returned from the new function was formatted with white spaces and tabs. And that contributed to the extra space. Tried using context.res.isRaw = true (which a 'Indicates that formatting is skipped for the response.' in documentation) and setting the content type to JSON also to text but it didn't make any difference. Do you guys know why it is behaving this way and any solution to it? Example: v1 used to send a json this way:

{'key_1':'value_1',......,'key_n':'value_n'}

Now the v2 sends back the same JSON (dots represents white spaces too):

{
....'key_1': 'value_1',
..........,
..........
'key_n': 'value_n'
}

Did anyone face a similar issue? I also noticed that it adds charset at the end of content type. Does that make any difference? V1: Content-Type: application/json; V2: Content-Type: application/json; charset=utf-8

Nesrine Hadj Khelil
  • 278
  • 1
  • 4
  • 13

1 Answers1

1

I have not seen any such issue so far. But, if you are sure this increase in size is just because of the addition of extra spaces in your JSON response, you would want to minify it before sending it as a response to the Web Application: Minify indented JSON string in .NET

In summary, you can use below Regex expression:

Regex.Replace(myJSON, "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1")

OR

Regex.Replace(myJSON, @"(""(?:[^""\\]|\\.)*"")|\s+", "$1")
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • This is close solution what I tried. I used a npm module serialize-javascript which does the same. Removes the white spaces minifies the json size. Will be accepting this answer. But would be better if Azure - function itself provided a option to do that instead of using a external library. – Taher Ghulam Mohammed Dec 11 '20 at 18:13