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
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")